1//===-- IPConstantPropagation.cpp - Propagate constants through calls -----===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// --- 5 unchanged lines hidden (view full) --- 14// to clean up the mess. 15// 16//===----------------------------------------------------------------------===// 17 18#define DEBUG_TYPE "ipconstprop" 19#include "llvm/Transforms/IPO.h" 20#include "llvm/Constants.h" 21#include "llvm/Instructions.h" |
22#include "llvm/LLVMContext.h" |
23#include "llvm/Module.h" 24#include "llvm/Pass.h" 25#include "llvm/Analysis/ValueTracking.h" 26#include "llvm/Support/CallSite.h" 27#include "llvm/Support/Compiler.h" 28#include "llvm/ADT/Statistic.h" 29#include "llvm/ADT/SmallVector.h" 30using namespace llvm; --- 94 unchanged lines hidden (view full) --- 125 } 126 127 // If we got to this point, there is a constant argument! 128 assert(NumNonconstant != ArgumentConstants.size()); 129 bool MadeChange = false; 130 Function::arg_iterator AI = F.arg_begin(); 131 for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI) { 132 // Do we have a constant argument? |
133 if (ArgumentConstants[i].second || AI->use_empty() || 134 (AI->hasByValAttr() && !F.onlyReadsMemory())) |
135 continue; 136 137 Value *V = ArgumentConstants[i].first; 138 if (V == 0) V = UndefValue::get(AI->getType()); 139 AI->replaceAllUsesWith(V); 140 ++NumArgumentsProped; 141 MadeChange = true; 142 } --- 5 unchanged lines hidden (view full) --- 148// all callers that use those return values with the constant value. This will 149// leave in the actual return values and instructions, but deadargelim will 150// clean that up. 151// 152// Additionally if a function always returns one of its arguments directly, 153// callers will be updated to use the value they pass in directly instead of 154// using the return value. 155bool IPCP::PropagateConstantReturn(Function &F) { |
156 if (F.getReturnType() == Type::getVoidTy(F.getContext())) |
157 return false; // No return value. 158 159 // If this function could be overridden later in the link stage, we can't 160 // propagate information about its results into callers. 161 if (F.mayBeOverridden()) 162 return false; |
163 164 LLVMContext &Context = F.getContext(); |
165 166 // Check to see if this function returns a constant. 167 SmallVector<Value *,4> RetVals; 168 const StructType *STy = dyn_cast<StructType>(F.getReturnType()); 169 if (STy) 170 for (unsigned i = 0, e = STy->getNumElements(); i < e; ++i) 171 RetVals.push_back(UndefValue::get(STy->getElementType(i))); 172 else --- 8 unchanged lines hidden (view full) --- 181 if (!RV) 182 continue; 183 184 // Find the returned value 185 Value *V; 186 if (!STy) 187 V = RI->getOperand(i); 188 else |
189 V = FindInsertedValue(RI->getOperand(0), i, Context); |
190 191 if (V) { 192 // Ignore undefs, we can change them into anything 193 if (isa<UndefValue>(V)) 194 continue; 195 196 // Try to see if all the rets return the same constant or argument. 197 if (isa<Constant>(V) || isa<Argument>(V)) { --- 80 unchanged lines hidden --- |