llvm.org GIT mirror llvm / 4ec2258
reapply r101434 with a fix for self-hosting rotate CallInst operands, i.e. move callee to the back of the operand array the motivation for this patch are laid out in my mail to llvm-commits: more efficient access to operands and callee, faster callgraph-construction, smaller compiler binary git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@101465 91177308-0d34-0410-b5e6-96231b3b80d8 Gabor Greif 9 years ago
44 changed file(s) with 592 addition(s) and 601 deletion(s). Raw diff Collapse all Expand all
10301030 /// indirect function invocation.
10311031 ///
10321032 Function *getCalledFunction() const {
1033 return dyn_cast(Op<0>());
1033 return dyn_cast(Op<-1>());
10341034 }
10351035
10361036 /// getCalledValue - Get a pointer to the function that is invoked by this
10371037 /// instruction.
1038 const Value *getCalledValue() const { return Op<0>(); }
1039 Value *getCalledValue() { return Op<0>(); }
1038 const Value *getCalledValue() const { return Op<-1>(); }
1039 Value *getCalledValue() { return Op<-1>(); }
10401040
10411041 /// setCalledFunction - Set the function called.
10421042 void setCalledFunction(Value* Fn) {
1043 Op<0>() = Fn;
1043 Op<-1>() = Fn;
10441044 }
10451045
10461046 // Methods for support type inquiry through isa, cast, and dyn_cast:
10701070 ->getElementType())->getReturnType(),
10711071 Instruction::Call,
10721072 OperandTraits::op_end(this) - (ArgEnd - ArgBegin + 1),
1073 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1073 unsigned(ArgEnd - ArgBegin + 1), InsertAtEnd) {
10741074 init(Func, ArgBegin, ArgEnd, NameStr,
10751075 typename std::iterator_traits::iterator_category());
10761076 }
4242 Intrinsic::ID getIntrinsicID() const {
4343 return (Intrinsic::ID)getCalledFunction()->getIntrinsicID();
4444 }
45
45
4646 // Methods for support type inquiry through isa, cast, and dyn_cast:
4747 static inline bool classof(const IntrinsicInst *) { return true; }
4848 static inline bool classof(const CallInst *I) {
7373 static inline bool classof(const Value *V) {
7474 return isa(V) && classof(cast(V));
7575 }
76
76
7777 static Value *StripCast(Value *C);
7878 };
7979
8282 class DbgDeclareInst : public DbgInfoIntrinsic {
8383 public:
8484 Value *getAddress() const;
85 MDNode *getVariable() const { return cast(getOperand(2)); }
85 MDNode *getVariable() const { return cast(getOperand(1)); }
8686
8787 // Methods for support type inquiry through isa, cast, and dyn_cast:
8888 static inline bool classof(const DbgDeclareInst *) { return true; }
102102 Value *getValue();
103103 uint64_t getOffset() const {
104104 return cast(
105 const_cast(getOperand(2)))->getZExtValue();
106 }
107 MDNode *getVariable() const { return cast(getOperand(3)); }
105 const_cast(getOperand(1)))->getZExtValue();
106 }
107 MDNode *getVariable() const { return cast(getOperand(2)); }
108108
109109 // Methods for support type inquiry through isa, cast, and dyn_cast:
110110 static inline bool classof(const DbgValueInst *) { return true; }
120120 ///
121121 class MemIntrinsic : public IntrinsicInst {
122122 public:
123 Value *getRawDest() const { return const_cast(getOperand(1)); }
124
125 Value *getLength() const { return const_cast(getOperand(3)); }
123 Value *getRawDest() const { return const_cast(getOperand(0)); }
124
125 Value *getLength() const { return const_cast(getOperand(2)); }
126126 ConstantInt *getAlignmentCst() const {
127 return cast(const_cast(getOperand(4)));
128 }
129
127 return cast(const_cast(getOperand(3)));
128 }
129
130130 unsigned getAlignment() const {
131131 return getAlignmentCst()->getZExtValue();
132132 }
133133
134134 ConstantInt *getVolatileCst() const {
135 return cast(const_cast(getOperand(5)));
135 return cast(const_cast(getOperand(4)));
136136 }
137137 bool isVolatile() const {
138138 return getVolatileCst()->getZExtValue() != 0;
148148 void setDest(Value *Ptr) {
149149 assert(getRawDest()->getType() == Ptr->getType() &&
150150 "setDest called with pointer of wrong type!");
151 setOperand(1, Ptr);
151 setOperand(0, Ptr);
152152 }
153153
154154 void setLength(Value *L) {
155155 assert(getLength()->getType() == L->getType() &&
156156 "setLength called with value of wrong type!");
157 setOperand(3, L);
158 }
159
157 setOperand(2, L);
158 }
159
160160 void setAlignment(Constant* A) {
161 setOperand(4, A);
161 setOperand(3, A);
162162 }
163163
164164 void setVolatile(Constant* V) {
165 setOperand(5, V);
165 setOperand(4, V);
166166 }
167167
168168 const Type *getAlignmentType() const {
169 return getOperand(4)->getType();
170 }
171
169 return getOperand(3)->getType();
170 }
171
172172 // Methods for support type inquiry through isa, cast, and dyn_cast:
173173 static inline bool classof(const MemIntrinsic *) { return true; }
174174 static inline bool classof(const IntrinsicInst *I) {
191191 public:
192192 /// get* - Return the arguments to the instruction.
193193 ///
194 Value *getValue() const { return const_cast(getOperand(2)); }
195
194 Value *getValue() const { return const_cast(getOperand(1)); }
195
196196 void setValue(Value *Val) {
197197 assert(getValue()->getType() == Val->getType() &&
198 "setSource called with pointer of wrong type!");
199 setOperand(2, Val);
200 }
201
198 "setValue called with value of wrong type!");
199 setOperand(1, Val);
200 }
201
202202 // Methods for support type inquiry through isa, cast, and dyn_cast:
203203 static inline bool classof(const MemSetInst *) { return true; }
204204 static inline bool classof(const IntrinsicInst *I) {
208208 return isa(V) && classof(cast(V));
209209 }
210210 };
211
211
212212 /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
213213 ///
214214 class MemTransferInst : public MemIntrinsic {
215215 public:
216216 /// get* - Return the arguments to the instruction.
217217 ///
218 Value *getRawSource() const { return const_cast(getOperand(2)); }
219
218 Value *getRawSource() const { return const_cast(getOperand(1)); }
219
220220 /// getSource - This is just like getRawSource, but it strips off any cast
221221 /// instructions that feed it, giving the original input. The returned
222222 /// value is guaranteed to be a pointer.
223223 Value *getSource() const { return getRawSource()->stripPointerCasts(); }
224
224
225225 void setSource(Value *Ptr) {
226226 assert(getRawSource()->getType() == Ptr->getType() &&
227227 "setSource called with pointer of wrong type!");
228 setOperand(2, Ptr);
229 }
230
228 setOperand(1, Ptr);
229 }
230
231231 // Methods for support type inquiry through isa, cast, and dyn_cast:
232232 static inline bool classof(const MemTransferInst *) { return true; }
233233 static inline bool classof(const IntrinsicInst *I) {
238238 return isa(V) && classof(cast(V));
239239 }
240240 };
241
242
241
242
243243 /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
244244 ///
245245 class MemCpyInst : public MemTransferInst {
281281 return isa(V) && classof(cast(V));
282282 }
283283 };
284
284
285285 /// MemoryUseIntrinsic - This is the common base class for the memory use
286286 /// marker intrinsics.
287287 ///
254254 private:
255255 /// Returns the operand number of the first argument
256256 unsigned getArgumentOffset() const {
257 if (isCall())
258 return 1; // Skip Function (ATM)
259 else
260257 return 0; // Args are at the front
261258 }
262259
263260 unsigned getArgumentEndOffset() const {
264261 if (isCall())
265 return 0; // Unchanged (ATM)
262 return 1; // Skip Function
266263 else
267264 return 3; // Skip BB, BB, Function
268265 }
269266
270267 IterTy getCallee() const {
271 // FIXME: this is slow, since we do not have the fast versions
272 // of the op_*() functions here. See CallSite::getCallee.
273 //
274 if (isCall())
275 return getInstruction()->op_begin(); // Unchanged (ATM)
276 else
277 return getInstruction()->op_end() - 3; // Skip BB, BB, Function
268 // FIXME: this is slow, since we do not have the fast versions
269 // of the op_*() functions here. See CallSite::getCallee.
270 //
271 return arg_end();
278272 }
279273 };
280274
9393 } else if (const CallInst* CI = extractMallocCall(V)) {
9494 if (!isArrayMalloc(V, &TD))
9595 // The size is the argument to the malloc call.
96 if (const ConstantInt* C = dyn_cast(CI->getOperand(1)))
96 if (const ConstantInt* C = dyn_cast(CI->getOperand(0)))
9797 return (C->getZExtValue() < Size);
9898 return false;
9999 } else if (const Argument *A = dyn_cast(V)) {
317317 case Intrinsic::memcpy:
318318 case Intrinsic::memmove: {
319319 unsigned Len = ~0U;
320 if (ConstantInt *LenCI = dyn_cast(II->getOperand(3)))
320 if (ConstantInt *LenCI = dyn_cast(II->getOperand(2)))
321321 Len = LenCI->getZExtValue();
322 Value *Dest = II->getOperand(1);
323 Value *Src = II->getOperand(2);
322 Value *Dest = II->getOperand(0);
323 Value *Src = II->getOperand(1);
324324 if (isNoAlias(Dest, Len, P, Size)) {
325325 if (isNoAlias(Src, Len, P, Size))
326326 return NoModRef;
331331 case Intrinsic::memset:
332332 // Since memset is 'accesses arguments' only, the AliasAnalysis base class
333333 // will handle it for the variable length case.
334 if (ConstantInt *LenCI = dyn_cast(II->getOperand(3))) {
334 if (ConstantInt *LenCI = dyn_cast(II->getOperand(2))) {
335335 unsigned Len = LenCI->getZExtValue();
336 Value *Dest = II->getOperand(1);
336 Value *Dest = II->getOperand(0);
337337 if (isNoAlias(Dest, Len, P, Size))
338338 return NoModRef;
339339 }
351351 case Intrinsic::atomic_load_umax:
352352 case Intrinsic::atomic_load_umin:
353353 if (TD) {
354 Value *Op1 = II->getOperand(1);
354 Value *Op1 = II->getOperand(0);
355355 unsigned Op1Size = TD->getTypeStoreSize(Op1->getType());
356356 if (isNoAlias(Op1, Op1Size, P, Size))
357357 return NoModRef;
360360 case Intrinsic::lifetime_start:
361361 case Intrinsic::lifetime_end:
362362 case Intrinsic::invariant_start: {
363 unsigned PtrSize = cast(II->getOperand(0))->getZExtValue();
364 if (isNoAlias(II->getOperand(1), PtrSize, P, Size))
365 return NoModRef;
366 break;
367 }
368 case Intrinsic::invariant_end: {
363369 unsigned PtrSize = cast(II->getOperand(1))->getZExtValue();
364370 if (isNoAlias(II->getOperand(2), PtrSize, P, Size))
365 return NoModRef;
366 break;
367 }
368 case Intrinsic::invariant_end: {
369 unsigned PtrSize = cast(II->getOperand(2))->getZExtValue();
370 if (isNoAlias(II->getOperand(3), PtrSize, P, Size))
371371 return NoModRef;
372372 break;
373373 }
771771 case Instruction::ICmp:
772772 case Instruction::FCmp: assert(0 && "Invalid for compares");
773773 case Instruction::Call:
774 if (Function *F = dyn_cast(Ops[0]))
774 if (Function *F = dyn_cast(Ops[NumOps - 1]))
775775 if (canConstantFoldCallTo(F))
776 return ConstantFoldCall(F, Ops+1, NumOps-1);
776 return ConstantFoldCall(F, Ops, NumOps - 1);
777777 return 0;
778778 case Instruction::PtrToInt:
779779 // If the input is a inttoptr, eliminate the pair. This requires knowing
251251 } else if (CallInst *CI = dyn_cast(*UI)) {
252252 // Make sure that this is just the function being called, not that it is
253253 // passing into the function.
254 for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i)
254 for (unsigned i = 0, e = CI->getNumOperands() - 1; i != e; ++i)
255255 if (CI->getOperand(i) == V) return true;
256256 } else if (InvokeInst *II = dyn_cast(*UI)) {
257257 // Make sure that this is just the function being called, not that it is
102102
103103 // If malloc calls' arg can be determined to be a multiple of ElementSize,
104104 // return the multiple. Otherwise, return NULL.
105 Value *MallocArg = CI->getOperand(1);
105 Value *MallocArg = CI->getOperand(0);
106106 Value *Multiple = NULL;
107107 if (ComputeMultiple(MallocArg, ElementSize, Multiple,
108108 LookThroughSExt))
119119 Value *ArraySize = computeArraySize(CI, TD);
120120
121121 if (ArraySize &&
122 ArraySize != ConstantInt::get(CI->getOperand(1)->getType(), 1))
122 ArraySize != ConstantInt::get(CI->getOperand(0)->getType(), 1))
123123 return CI;
124124
125125 // CI is a non-array malloc or we can't figure out that it is an array malloc.
116116 Pointer = V->getOperand(0);
117117 PointerSize = AA->getTypeStoreSize(V->getType());
118118 } else if (isFreeCall(Inst)) {
119 Pointer = Inst->getOperand(1);
119 Pointer = Inst->getOperand(0);
120120 // calls to free() erase the entire structure
121121 PointerSize = ~0ULL;
122122 } else if (isa(Inst) || isa(Inst)) {
196196 // pointer, not on query pointers that are indexed off of them. It'd
197197 // be nice to handle that at some point.
198198 AliasAnalysis::AliasResult R =
199 AA->alias(II->getOperand(3), ~0U, MemPtr, ~0U);
199 AA->alias(II->getOperand(2), ~0U, MemPtr, ~0U);
200200 if (R == AliasAnalysis::MustAlias) {
201 InvariantTag = II->getOperand(1);
201 InvariantTag = II->getOperand(0);
202202 continue;
203203 }
204204
209209 // pointer, not on query pointers that are indexed off of them. It'd
210210 // be nice to handle that at some point.
211211 AliasAnalysis::AliasResult R =
212 AA->alias(II->getOperand(2), ~0U, MemPtr, ~0U);
212 AA->alias(II->getOperand(1), ~0U, MemPtr, ~0U);
213213 if (R == AliasAnalysis::MustAlias)
214214 return MemDepResult::getDef(II);
215215 }
365365 MemSize = AA->getTypeStoreSize(LI->getType());
366366 }
367367 } else if (isFreeCall(QueryInst)) {
368 MemPtr = QueryInst->getOperand(1);
368 MemPtr = QueryInst->getOperand(0);
369369 // calls to free() erase the entire structure, not just a field.
370370 MemSize = ~0UL;
371371 } else if (isa(QueryInst) || isa(QueryInst)) {
377377 case Intrinsic::lifetime_start:
378378 case Intrinsic::lifetime_end:
379379 case Intrinsic::invariant_start:
380 MemPtr = QueryInst->getOperand(1);
381 MemSize = cast(QueryInst->getOperand(0))->getZExtValue();
382 break;
383 case Intrinsic::invariant_end:
380384 MemPtr = QueryInst->getOperand(2);
381385 MemSize = cast(QueryInst->getOperand(1))->getZExtValue();
382 break;
383 case Intrinsic::invariant_end:
384 MemPtr = QueryInst->getOperand(3);
385 MemSize = cast(QueryInst->getOperand(2))->getZExtValue();
386386 break;
387387 default:
388388 CallSite QueryCS = CallSite::get(QueryInst);
952952 if (const IntrinsicInst *II = dyn_cast(I))
953953 // sqrt(-0.0) = -0.0, no other negative results are possible.
954954 if (II->getIntrinsicID() == Intrinsic::sqrt)
955 return CannotBeNegativeZero(II->getOperand(1), Depth+1);
955 return CannotBeNegativeZero(II->getOperand(0), Depth+1);
956956
957957 if (const CallInst *CI = dyn_cast(I))
958958 if (const Function *F = CI->getCalledFunction()) {
965965 if (F->getName() == "fabsl") return true;
966966 if (F->getName() == "sqrt" || F->getName() == "sqrtf" ||
967967 F->getName() == "sqrtl")
968 return CannotBeNegativeZero(CI->getOperand(1), Depth+1);
968 return CannotBeNegativeZero(CI->getOperand(0), Depth+1);
969969 }
970970 }
971971
11331133 Vals.push_back(cast(I).isVolatile());
11341134 break;
11351135 case Instruction::Call: {
1136 const PointerType *PTy = cast(I.getOperand(0)->getType());
1136 const CallInst &CI = cast(I);
1137 const PointerType *PTy = cast(CI.getCalledValue()->getType());
11371138 const FunctionType *FTy = cast(PTy->getElementType());
11381139
11391140 Code = bitc::FUNC_CODE_INST_CALL;
11401141
1141 const CallInst *CI = cast(&I);
1142 Vals.push_back(VE.getAttributeID(CI->getAttributes()));
1143 Vals.push_back((CI->getCallingConv() << 1) | unsigned(CI->isTailCall()));
1144 PushValueAndType(CI->getOperand(0), InstID, Vals, VE); // Callee
1142 Vals.push_back(VE.getAttributeID(CI.getAttributes()));
1143 Vals.push_back((CI.getCallingConv() << 1) | unsigned(CI.isTailCall()));
1144 PushValueAndType(CI.getCalledValue(), InstID, Vals, VE); // Callee
11451145
11461146 // Emit value #'s for the fixed parameters.
11471147 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
1148 Vals.push_back(VE.getValueID(I.getOperand(i+1))); // fixed param.
1148 Vals.push_back(VE.getValueID(I.getOperand(i))); // fixed param.
11491149
11501150 // Emit type/value pairs for varargs params.
11511151 if (FTy->isVarArg()) {
1152 unsigned NumVarargs = I.getNumOperands()-1-FTy->getNumParams();
1153 for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands();
1152 for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-1;
11541153 i != e; ++i)
11551154 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // varargs
11561155 }
197197 bool IsCleanUp = (NumOps == 3);
198198
199199 if (!IsCleanUp)
200 if (ConstantInt *CI = dyn_cast(SI->getOperand(3)))
200 if (ConstantInt *CI = dyn_cast(SI->getOperand(2)))
201201 IsCleanUp = (CI->getZExtValue() == 0);
202202
203203 if (IsCleanUp)
236236 if (!Sel || Sel->getParent()->getParent() != F) continue;
237237
238238 // Index of the ".llvm.eh.catch.all.value" variable.
239 unsigned OpIdx = Sel->getNumOperands() - 1;
239 unsigned OpIdx = Sel->getNumOperands() - 2;
240240 GlobalVariable *GV = dyn_cast(Sel->getOperand(OpIdx));
241241 if (GV != EHCatchAllValue) continue;
242242 Sel->setOperand(OpIdx, EHCatchAllValue->getInitializer());
365365 bool IsCleanUp = (NumOps == 3);
366366
367367 if (!IsCleanUp)
368 if (ConstantInt *CI = dyn_cast(II->getOperand(3)))
368 if (ConstantInt *CI = dyn_cast(II->getOperand(2)))
369369 IsCleanUp = (CI->getZExtValue() == 0);
370370
371371 if (IsCleanUp)
389389
390390 // Use the exception object pointer and the personality function
391391 // from the original selector.
392 Args.push_back(II->getOperand(1)); // Exception object pointer.
393 Args.push_back(II->getOperand(2)); // Personality function.
392 Args.push_back(II->getOperand(0)); // Exception object pointer.
393 Args.push_back(II->getOperand(1)); // Personality function.
394394 Args.push_back(EHCatchAllValue->getInitializer()); // Catch-all indicator.
395395
396396 CallInst *NewSelector =
270270 case Intrinsic::gcwrite:
271271 if (LowerWr) {
272272 // Replace a write barrier with a simple store.
273 Value *St = new StoreInst(CI->getOperand(1), CI->getOperand(3), CI);
273 Value *St = new StoreInst(CI->getOperand(0), CI->getOperand(2), CI);
274274 CI->replaceAllUsesWith(St);
275275 CI->eraseFromParent();
276276 }
278278 case Intrinsic::gcread:
279279 if (LowerRd) {
280280 // Replace a read barrier with a simple load.
281 Value *Ld = new LoadInst(CI->getOperand(2), "", CI);
281 Value *Ld = new LoadInst(CI->getOperand(1), "", CI);
282282 Ld->takeName(CI);
283283 CI->replaceAllUsesWith(Ld);
284284 CI->eraseFromParent();
289289 // Initialize the GC root, but do not delete the intrinsic. The
290290 // backend needs the intrinsic to flag the stack slot.
291291 Roots.push_back(cast(
292 CI->getOperand(1)->stripPointerCasts()));
292 CI->getOperand(0)->stripPointerCasts()));
293293 }
294294 break;
295295 default:
307307 static void ReplaceFPIntrinsicWithCall(CallInst *CI, const char *Fname,
308308 const char *Dname,
309309 const char *LDname) {
310 switch (CI->getOperand(1)->getType()->getTypeID()) {
310 switch (CI->getOperand(0)->getType()->getTypeID()) {
311311 default: llvm_unreachable("Invalid type in intrinsic");
312312 case Type::FloatTyID:
313 ReplaceCallWith(Fname, CI, CI->op_begin() + 1, CI->op_end(),
313 ReplaceCallWith(Fname, CI, CI->op_begin(), CI->op_end() - 1,
314314 Type::getFloatTy(CI->getContext()));
315315 break;
316316 case Type::DoubleTyID:
317 ReplaceCallWith(Dname, CI, CI->op_begin() + 1, CI->op_end(),
317 ReplaceCallWith(Dname, CI, CI->op_begin(), CI->op_end() - 1,
318318 Type::getDoubleTy(CI->getContext()));
319319 break;
320320 case Type::X86_FP80TyID:
321321 case Type::FP128TyID:
322322 case Type::PPC_FP128TyID:
323 ReplaceCallWith(LDname, CI, CI->op_begin() + 1, CI->op_end(),
324 CI->getOperand(1)->getType());
323 ReplaceCallWith(LDname, CI, CI->op_begin(), CI->op_end() - 1,
324 CI->getOperand(0)->getType());
325325 break;
326326 }
327327 }
346346 // by the lowerinvoke pass. In both cases, the right thing to do is to
347347 // convert the call to an explicit setjmp or longjmp call.
348348 case Intrinsic::setjmp: {
349 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin() + 1, CI->op_end(),
349 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin(), CI->op_end() - 1,
350350 Type::getInt32Ty(Context));
351351 if (!CI->getType()->isVoidTy())
352352 CI->replaceAllUsesWith(V);
358358 break;
359359
360360 case Intrinsic::longjmp: {
361 ReplaceCallWith("longjmp", CI, CI->op_begin() + 1, CI->op_end(),
361 ReplaceCallWith("longjmp", CI, CI->op_begin(), CI->op_end() - 1,
362362 Type::getVoidTy(Context));
363363 break;
364364 }
370370 break;
371371 }
372372 case Intrinsic::ctpop:
373 CI->replaceAllUsesWith(LowerCTPOP(Context, CI->getOperand(1), CI));
373 CI->replaceAllUsesWith(LowerCTPOP(Context, CI->getOperand(0), CI));
374374 break;
375375
376376 case Intrinsic::bswap:
377 CI->replaceAllUsesWith(LowerBSWAP(Context, CI->getOperand(1), CI));
377 CI->replaceAllUsesWith(LowerBSWAP(Context, CI->getOperand(0), CI));
378378 break;
379379
380380 case Intrinsic::ctlz:
381 CI->replaceAllUsesWith(LowerCTLZ(Context, CI->getOperand(1), CI));
381 CI->replaceAllUsesWith(LowerCTLZ(Context, CI->getOperand(0), CI));
382382 break;
383383
384384 case Intrinsic::cttz: {
385385 // cttz(x) -> ctpop(~X & (X-1))
386 Value *Src = CI->getOperand(1);
386 Value *Src = CI->getOperand(0);
387387 Value *NotSrc = Builder.CreateNot(Src);
388388 NotSrc->setName(Src->getName() + ".not");
389389 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
444444
445445 case Intrinsic::memcpy: {
446446 const IntegerType *IntPtr = TD.getIntPtrType(Context);
447 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
447 Value *Size = Builder.CreateIntCast(CI->getOperand(2), IntPtr,
448448 /* isSigned */ false);
449449 Value *Ops[3];
450 Ops[0] = CI->getOperand(1);
451 Ops[1] = CI->getOperand(2);
450 Ops[0] = CI->getOperand(0);
451 Ops[1] = CI->getOperand(1);
452452 Ops[2] = Size;
453 ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getOperand(1)->getType());
453 ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getOperand(0)->getType());
454454 break;
455455 }
456456 case Intrinsic::memmove: {
457457 const IntegerType *IntPtr = TD.getIntPtrType(Context);
458 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
458 Value *Size = Builder.CreateIntCast(CI->getOperand(2), IntPtr,
459459 /* isSigned */ false);
460460 Value *Ops[3];
461 Ops[0] = CI->getOperand(1);
462 Ops[1] = CI->getOperand(2);
461 Ops[0] = CI->getOperand(0);
462 Ops[1] = CI->getOperand(1);
463463 Ops[2] = Size;
464 ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getOperand(1)->getType());
464 ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getOperand(0)->getType());
465465 break;
466466 }
467467 case Intrinsic::memset: {
468468 const IntegerType *IntPtr = TD.getIntPtrType(Context);
469 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
469 Value *Size = Builder.CreateIntCast(CI->getOperand(2), IntPtr,
470470 /* isSigned */ false);
471471 Value *Ops[3];
472 Ops[0] = CI->getOperand(1);
472 Ops[0] = CI->getOperand(0);
473473 // Extend the amount to i32.
474 Ops[1] = Builder.CreateIntCast(CI->getOperand(2), Type::getInt32Ty(Context),
474 Ops[1] = Builder.CreateIntCast(CI->getOperand(1), Type::getInt32Ty(Context),
475475 /* isSigned */ false);
476476 Ops[2] = Size;
477 ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getOperand(1)->getType());
477 ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getOperand(0)->getType());
478478 break;
479479 }
480480 case Intrinsic::sqrt: {
3030 #include "llvm/Target/TargetData.h"
3131 #include "llvm/Target/TargetFrameInfo.h"
3232 #include "llvm/Target/TargetInstrInfo.h"
33 #include "llvm/Target/TargetIntrinsicInfo.h"
3433 #include "llvm/Target/TargetLowering.h"
3534 #include "llvm/Target/TargetOptions.h"
3635 #include "llvm/Support/Compiler.h"
309308 void llvm::AddCatchInfo(const CallInst &I, MachineModuleInfo *MMI,
310309 MachineBasicBlock *MBB) {
311310 // Inform the MachineModuleInfo of the personality for this landing pad.
312 const ConstantExpr *CE = cast(I.getOperand(2));
311 const ConstantExpr *CE = cast(I.getOperand(1));
313312 assert(CE->getOpcode() == Instruction::BitCast &&
314313 isa(CE->getOperand(0)) &&
315314 "Personality should be a function");
318317 // Gather all the type infos for this landing pad and pass them along to
319318 // MachineModuleInfo.
320319 std::vector TyInfo;
321 unsigned N = I.getNumOperands();
322
323 for (unsigned i = N - 1; i > 2; --i) {
320 unsigned N = I.getNumOperands() - 1;
321
322 for (unsigned i = N - 1; i > 1; --i) {
324323 if (const ConstantInt *CI = dyn_cast(I.getOperand(i))) {
325324 unsigned FilterLength = CI->getZExtValue();
326325 unsigned FirstCatch = i + FilterLength + !FilterLength;
350349 }
351350 }
352351
353 if (N > 3) {
354 TyInfo.reserve(N - 3);
355 for (unsigned j = 3; j < N; ++j)
352 if (N > 2) {
353 TyInfo.reserve(N - 2);
354 for (unsigned j = 2; j < N; ++j)
356355 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
357356 MMI->addCatchTypeInfo(MBB, TyInfo);
358357 }
27702770 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
27712771
27722772 // Add all operands of the call to the operand list.
2773 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
2773 for (unsigned i = 0, e = I.getNumOperands()-1; i != e; ++i) {
27742774 SDValue Op = getValue(I.getOperand(i));
27752775 assert(TLI.isTypeLegal(Op.getValueType()) &&
27762776 "Intrinsic uses a non-legal type?");
28762876 SDValue Root = getRoot();
28772877 SDValue L =
28782878 DAG.getAtomic(Op, getCurDebugLoc(),
2879 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
2879 getValue(I.getOperand(1)).getValueType().getSimpleVT(),
28802880 Root,
2881 getValue(I.getOperand(0)),
28812882 getValue(I.getOperand(1)),
2882 getValue(I.getOperand(2)),
2883 I.getOperand(1));
2883 I.getOperand(0));
28842884 setValue(&I, L);
28852885 DAG.setRoot(L.getValue(1));
28862886 return 0;
28892889 // implVisitAluOverflow - Lower arithmetic overflow instrinsics.
28902890 const char *
28912891 SelectionDAGBuilder::implVisitAluOverflow(const CallInst &I, ISD::NodeType Op) {
2892 SDValue Op1 = getValue(I.getOperand(1));
2893 SDValue Op2 = getValue(I.getOperand(2));
2892 SDValue Op1 = getValue(I.getOperand(0));
2893 SDValue Op2 = getValue(I.getOperand(1));
28942894
28952895 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
28962896 setValue(&I, DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2));
29042904 SDValue result;
29052905 DebugLoc dl = getCurDebugLoc();
29062906
2907 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
2907 if (getValue(I.getOperand(0)).getValueType() == MVT::f32 &&
29082908 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
2909 SDValue Op = getValue(I.getOperand(1));
2909 SDValue Op = getValue(I.getOperand(0));
29102910
29112911 // Put the exponent in the right bit position for later addition to the
29122912 // final result:
30163016 } else {
30173017 // No special expansion.
30183018 result = DAG.getNode(ISD::FEXP, dl,
3019 getValue(I.getOperand(1)).getValueType(),
3020 getValue(I.getOperand(1)));
3019 getValue(I.getOperand(0)).getValueType(),
3020 getValue(I.getOperand(0)));
30213021 }
30223022
30233023 setValue(&I, result);
30303030 SDValue result;
30313031 DebugLoc dl = getCurDebugLoc();
30323032
3033 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
3033 if (getValue(I.getOperand(0)).getValueType() == MVT::f32 &&
30343034 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3035 SDValue Op = getValue(I.getOperand(1));
3035 SDValue Op = getValue(I.getOperand(0));
30363036 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
30373037
30383038 // Scale the exponent by log(2) [0.69314718f].
31263126 } else {
31273127 // No special expansion.
31283128 result = DAG.getNode(ISD::FLOG, dl,
3129 getValue(I.getOperand(1)).getValueType(),
3130 getValue(I.getOperand(1)));
3129 getValue(I.getOperand(0)).getValueType(),
3130 getValue(I.getOperand(0)));
31313131 }
31323132
31333133 setValue(&I, result);
31403140 SDValue result;
31413141 DebugLoc dl = getCurDebugLoc();
31423142
3143 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
3143 if (getValue(I.getOperand(0)).getValueType() == MVT::f32 &&
31443144 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3145 SDValue Op = getValue(I.getOperand(1));
3145 SDValue Op = getValue(I.getOperand(0));
31463146 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
31473147
31483148 // Get the exponent.
32353235 } else {
32363236 // No special expansion.
32373237 result = DAG.getNode(ISD::FLOG2, dl,
3238 getValue(I.getOperand(1)).getValueType(),
3239 getValue(I.getOperand(1)));
3238 getValue(I.getOperand(0)).getValueType(),
3239 getValue(I.getOperand(0)));
32403240 }
32413241
32423242 setValue(&I, result);
32493249 SDValue result;
32503250 DebugLoc dl = getCurDebugLoc();
32513251
3252 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
3252 if (getValue(I.getOperand(0)).getValueType() == MVT::f32 &&
32533253 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3254 SDValue Op = getValue(I.getOperand(1));
3254 SDValue Op = getValue(I.getOperand(0));
32553255 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
32563256
32573257 // Scale the exponent by log10(2) [0.30102999f].
33373337 } else {
33383338 // No special expansion.
33393339 result = DAG.getNode(ISD::FLOG10, dl,
3340 getValue(I.getOperand(1)).getValueType(),
3341 getValue(I.getOperand(1)));
3340 getValue(I.getOperand(0)).getValueType(),
3341 getValue(I.getOperand(0)));
33423342 }
33433343
33443344 setValue(&I, result);
33513351 SDValue result;
33523352 DebugLoc dl = getCurDebugLoc();
33533353
3354 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
3354 if (getValue(I.getOperand(0)).getValueType() == MVT::f32 &&
33553355 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3356 SDValue Op = getValue(I.getOperand(1));
3356 SDValue Op = getValue(I.getOperand(0));
33573357
33583358 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
33593359
34513451 } else {
34523452 // No special expansion.
34533453 result = DAG.getNode(ISD::FEXP2, dl,
3454 getValue(I.getOperand(1)).getValueType(),
3455 getValue(I.getOperand(1)));
3454 getValue(I.getOperand(0)).getValueType(),
3455 getValue(I.getOperand(0)));
34563456 }
34573457
34583458 setValue(&I, result);
34633463 void
34643464 SelectionDAGBuilder::visitPow(const CallInst &I) {
34653465 SDValue result;
3466 const Value *Val = I.getOperand(1);
3466 const Value *Val = I.getOperand(0);
34673467 DebugLoc dl = getCurDebugLoc();
34683468 bool IsExp10 = false;
34693469
34703470 if (getValue(Val).getValueType() == MVT::f32 &&
3471 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
3471 getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
34723472 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
34733473 if (Constant *C = const_cast(dyn_cast(Val))) {
34743474 if (ConstantFP *CFP = dyn_cast(C)) {
34793479 }
34803480
34813481 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3482 SDValue Op = getValue(I.getOperand(2));
3482 SDValue Op = getValue(I.getOperand(1));
34833483
34843484 // Put the exponent in the right bit position for later addition to the
34853485 // final result:
35843584 } else {
35853585 // No special expansion.
35863586 result = DAG.getNode(ISD::FPOW, dl,
3587 getValue(I.getOperand(1)).getValueType(),
3588 getValue(I.getOperand(1)),
3589 getValue(I.getOperand(2)));
3587 getValue(I.getOperand(0)).getValueType(),
3588 getValue(I.getOperand(0)),
3589 getValue(I.getOperand(1)));
35903590 }
35913591
35923592 setValue(&I, result);
36643664 case Intrinsic::vacopy: visitVACopy(I); return 0;
36653665 case Intrinsic::returnaddress:
36663666 setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
3667 getValue(I.getOperand(1))));
3667 getValue(I.getOperand(0))));
36683668 return 0;
36693669 case Intrinsic::frameaddress:
36703670 setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
3671 getValue(I.getOperand(1))));
3671 getValue(I.getOperand(0))));
36723672 return 0;
36733673 case Intrinsic::setjmp:
36743674 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
36773677 case Intrinsic::memcpy: {
36783678 // Assert for address < 256 since we support only user defined address
36793679 // spaces.
3680 assert(cast(I.getOperand(1)->getType())->getAddressSpace()
3680 assert(cast(I.getOperand(0)->getType())->getAddressSpace()
36813681 < 256 &&
3682 cast(I.getOperand(2)->getType())->getAddressSpace()
3682 cast(I.getOperand(1)->getType())->getAddressSpace()
36833683 < 256 &&
36843684 "Unknown address space");
3685 SDValue Op1 = getValue(I.getOperand(1));
3686 SDValue Op2 = getValue(I.getOperand(2));
3687 SDValue Op3 = getValue(I.getOperand(3));
3688 unsigned Align = cast(I.getOperand(4))->getZExtValue();
3689 bool isVol = cast(I.getOperand(5))->getZExtValue();
3685 SDValue Op1 = getValue(I.getOperand(0));
3686 SDValue Op2 = getValue(I.getOperand(1));
3687 SDValue Op3 = getValue(I.getOperand(2));
3688 unsigned Align = cast(I.getOperand(3))->getZExtValue();
3689 bool isVol = cast(I.getOperand(4))->getZExtValue();
36903690 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol, false,
3691 I.getOperand(1), 0, I.getOperand(2), 0));
3691 I.getOperand(0), 0, I.getOperand(1), 0));
36923692 return 0;
36933693 }
36943694 case Intrinsic::memset: {
36953695 // Assert for address < 256 since we support only user defined address
36963696 // spaces.
3697 assert(cast(I.getOperand(1)->getType())->getAddressSpace()
3697 assert(cast(I.getOperand(0)->getType())->getAddressSpace()
36983698 < 256 &&
36993699 "Unknown address space");
3700 SDValue Op1 = getValue(I.getOperand(1));
3701 SDValue Op2 = getValue(I.getOperand(2));
3702 SDValue Op3 = getValue(I.getOperand(3));
3703 unsigned Align = cast(I.getOperand(4))->getZExtValue();
3704 bool isVol = cast(I.getOperand(5))->getZExtValue();
3700 SDValue Op1 = getValue(I.getOperand(0));
3701 SDValue Op2 = getValue(I.getOperand(1));
3702 SDValue Op3 = getValue(I.getOperand(2));
3703 unsigned Align = cast(I.getOperand(3))->getZExtValue();
3704 bool isVol = cast(I.getOperand(4))->getZExtValue();
37053705 DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
3706 I.getOperand(1), 0));
3706 I.getOperand(0), 0));
37073707 return 0;
37083708 }
37093709 case Intrinsic::memmove: {
37103710 // Assert for address < 256 since we support only user defined address
37113711 // spaces.
3712 assert(cast(I.getOperand(1)->getType())->getAddressSpace()
3712 assert(cast(I.getOperand(0)->getType())->getAddressSpace()
37133713 < 256 &&
3714 cast(I.getOperand(2)->getType())->getAddressSpace()
3714 cast(I.getOperand(1)->getType())->getAddressSpace()
37153715 < 256 &&
37163716 "Unknown address space");
3717 SDValue Op1 = getValue(I.getOperand(1));
3718 SDValue Op2 = getValue(I.getOperand(2));
3719 SDValue Op3 = getValue(I.getOperand(3));
3720 unsigned Align = cast(I.getOperand(4))->getZExtValue();
3721 bool isVol = cast(I.getOperand(5))->getZExtValue();
3717 SDValue Op1 = getValue(I.getOperand(0));
3718 SDValue Op2 = getValue(I.getOperand(1));
3719 SDValue Op3 = getValue(I.getOperand(2));
3720 unsigned Align = cast(I.getOperand(3))->getZExtValue();
3721 bool isVol = cast(I.getOperand(4))->getZExtValue();
37223722
37233723 // If the source and destination are known to not be aliases, we can
37243724 // lower memmove as memcpy.
37253725 uint64_t Size = -1ULL;
37263726 if (ConstantSDNode *C = dyn_cast(Op3))
37273727 Size = C->getZExtValue();
3728 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
3728 if (AA->alias(I.getOperand(0), Size, I.getOperand(1), Size) ==
37293729 AliasAnalysis::NoAlias) {
37303730 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
3731 false, I.getOperand(1), 0, I.getOperand(2), 0));
3731 false, I.getOperand(0), 0, I.getOperand(1), 0));
37323732 return 0;
37333733 }
37343734
37353735 DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
3736 I.getOperand(1), 0, I.getOperand(2), 0));
3736 I.getOperand(0), 0, I.getOperand(1), 0));
37373737 return 0;
37383738 }
37393739 case Intrinsic::dbg_declare: {
38453845 // Insert the EHSELECTION instruction.
38463846 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
38473847 SDValue Ops[2];
3848 Ops[0] = getValue(I.getOperand(1));
3848 Ops[0] = getValue(I.getOperand(0));
38493849 Ops[1] = getRoot();
38503850 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
38513851 DAG.setRoot(Op.getValue(1));
38553855
38563856 case Intrinsic::eh_typeid_for: {
38573857 // Find the type id for the given typeinfo.
3858 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
3858 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(0));
38593859 unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV);
38603860 Res = DAG.getConstant(TypeID, MVT::i32);
38613861 setValue(&I, Res);
38683868 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
38693869 MVT::Other,
38703870 getControlRoot(),
3871 getValue(I.getOperand(1)),
3872 getValue(I.getOperand(2))));
3871 getValue(I.getOperand(0)),
3872 getValue(I.getOperand(1))));
38733873 return 0;
38743874 case Intrinsic::eh_unwind_init:
38753875 DAG.getMachineFunction().getMMI().setCallsUnwindInit(true);
38763876 return 0;
38773877 case Intrinsic::eh_dwarf_cfa: {
3878 EVT VT = getValue(I.getOperand(1)).getValueType();
3879 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
3878 EVT VT = getValue(I.getOperand(0)).getValueType();
3879 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(0)), dl,
38803880 TLI.getPointerTy());
38813881 SDValue Offset = DAG.getNode(ISD::ADD, dl,
38823882 TLI.getPointerTy(),
38923892 }
38933893 case Intrinsic::eh_sjlj_callsite: {
38943894 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
3895 ConstantInt *CI = dyn_cast(I.getOperand(1));
3895 ConstantInt *CI = dyn_cast(I.getOperand(0));
38963896 assert(CI && "Non-constant call site value in eh.sjlj.callsite!");
38973897 assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
38983898
39223922 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
39233923 }
39243924 EVT DestVT = TLI.getValueType(I.getType());
3925 const Value *Op1 = I.getOperand(1);
3925 const Value *Op1 = I.getOperand(0);
39263926 Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
39273927 DAG.getValueType(DestVT),
39283928 DAG.getValueType(getValue(Op1).getValueType()),
3929 getValue(I.getOperand(1)),
39293930 getValue(I.getOperand(2)),
3930 getValue(I.getOperand(3)),
39313931 Code);
39323932 setValue(&I, Res);
39333933 return 0;
39343934 }
39353935 case Intrinsic::sqrt:
39363936 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
3937 getValue(I.getOperand(1)).getValueType(),
3938 getValue(I.getOperand(1))));
3937 getValue(I.getOperand(0)).getValueType(),
3938 getValue(I.getOperand(0))));
39393939 return 0;
39403940 case Intrinsic::powi:
3941 setValue(&I, ExpandPowI(dl, getValue(I.getOperand(1)),
3942 getValue(I.getOperand(2)), DAG));
3941 setValue(&I, ExpandPowI(dl, getValue(I.getOperand(0)),
3942 getValue(I.getOperand(1)), DAG));
39433943 return 0;
39443944 case Intrinsic::sin:
39453945 setValue(&I, DAG.getNode(ISD::FSIN, dl,
3946 getValue(I.getOperand(1)).getValueType(),
3947 getValue(I.getOperand(1))));
3946 getValue(I.getOperand(0)).getValueType(),
3947 getValue(I.getOperand(0))));
39483948 return 0;
39493949 case Intrinsic::cos:
39503950 setValue(&I, DAG.getNode(ISD::FCOS, dl,
3951 getValue(I.getOperand(1)).getValueType(),
3952 getValue(I.getOperand(1))));
3951 getValue(I.getOperand(0)).getValueType(),
3952 getValue(I.getOperand(0))));
39533953 return 0;
39543954 case Intrinsic::log:
39553955 visitLog(I);
39713971 return 0;
39723972 case Intrinsic::convert_to_fp16:
39733973 setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, dl,
3974 MVT::i16, getValue(I.getOperand(1))));
3974 MVT::i16, getValue(I.getOperand(0))));
39753975 return 0;
39763976 case Intrinsic::convert_from_fp16:
39773977 setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, dl,
3978 MVT::f32, getValue(I.getOperand(1))));
3978 MVT::f32, getValue(I.getOperand(0))));
39793979 return 0;
39803980 case Intrinsic::pcmarker: {
3981 SDValue Tmp = getValue(I.getOperand(1));
3981 SDValue Tmp = getValue(I.getOperand(0));
39823982 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
39833983 return 0;
39843984 }
39933993 }
39943994 case Intrinsic::bswap:
39953995 setValue(&I, DAG.getNode(ISD::BSWAP, dl,
3996 getValue(I.getOperand(1)).getValueType(),
3997 getValue(I.getOperand(1))));
3996 getValue(I.getOperand(0)).getValueType(),
3997 getValue(I.getOperand(0))));
39983998 return 0;
39993999 case Intrinsic::cttz: {
4000 SDValue Arg = getValue(I.getOperand(1));
4000 SDValue Arg = getValue(I.getOperand(0));
40014001 EVT Ty = Arg.getValueType();
40024002 setValue(&I, DAG.getNode(ISD::CTTZ, dl, Ty, Arg));
40034003 return 0;
40044004 }
40054005 case Intrinsic::ctlz: {
4006 SDValue Arg = getValue(I.getOperand(1));
4006 SDValue Arg = getValue(I.getOperand(0));
40074007 EVT Ty = Arg.getValueType();
40084008 setValue(&I, DAG.getNode(ISD::CTLZ, dl, Ty, Arg));
40094009 return 0;
40104010 }
40114011 case Intrinsic::ctpop: {
4012 SDValue Arg = getValue(I.getOperand(1));
4012 SDValue Arg = getValue(I.getOperand(0));
40134013 EVT Ty = Arg.getValueType();
40144014 setValue(&I, DAG.getNode(ISD::CTPOP, dl, Ty, Arg));
40154015 return 0;
40234023 return 0;
40244024 }
40254025 case Intrinsic::stackrestore: {
4026 Res = getValue(I.getOperand(1));
4026 Res = getValue(I.getOperand(0));
40274027 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res));
40284028 return 0;
40294029 }
40334033 MachineFrameInfo *MFI = MF.getFrameInfo();
40344034 EVT PtrTy = TLI.getPointerTy();
40354035
4036 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4037 AllocaInst *Slot = cast(I.getOperand(2));
4036 SDValue Src = getValue(I.getOperand(0)); // The guard's value.
4037 AllocaInst *Slot = cast(I.getOperand(1));
40384038
40394039 int FI = FuncInfo.StaticAllocaMap[Slot];
40404040 MFI->setStackProtectorIndex(FI);
40514051 }
40524052 case Intrinsic::objectsize: {
40534053 // If we don't know by now, we're never going to know.
4054 ConstantInt *CI = dyn_cast(I.getOperand(2));
4054 ConstantInt *CI = dyn_cast(I.getOperand(1));
40554055
40564056 assert(CI && "Non-constant type in __builtin_object_size?");
40574057
4058 SDValue Arg = getValue(I.getOperand(0));
4058 SDValue Arg = getValue(I.getCalledValue());
40594059 EVT Ty = Arg.getValueType();
40604060
40614061 if (CI->getZExtValue() == 0)
40714071 return 0;
40724072
40734073 case Intrinsic::init_trampoline: {
4074 const Function *F = cast(I.getOperand(2)->stripPointerCasts());
4074 const Function *F = cast(I.getOperand(1)->stripPointerCasts());
40754075
40764076 SDValue Ops[6];
40774077 Ops[0] = getRoot();
4078 Ops[1] = getValue(I.getOperand(1));
4079 Ops[2] = getValue(I.getOperand(2));
4080 Ops[3] = getValue(I.getOperand(3));
4081 Ops[4] = DAG.getSrcValue(I.getOperand(1));
4078 Ops[1] = getValue(I.getOperand(0));
4079 Ops[2] = getValue(I.getOperand(1));
4080 Ops[3] = getValue(I.getOperand(2));
4081 Ops[4] = DAG.getSrcValue(I.getOperand(0));
40824082 Ops[5] = DAG.getSrcValue(F);
40834083
40844084 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
40914091 }
40924092 case Intrinsic::gcroot:
40934093 if (GFI) {
4094 const Value *Alloca = I.getOperand(1);
4095 const Constant *TypeMap = cast(I.getOperand(2));
4094 const Value *Alloca = I.getOperand(0);
4095 const Constant *TypeMap = cast(I.getOperand(1));
40964096
40974097 FrameIndexSDNode *FI = cast(getValue(Alloca).getNode());
40984098 GFI->addStackRoot(FI->getIndex(), TypeMap);
41244124 case Intrinsic::prefetch: {
41254125 SDValue Ops[4];
41264126 Ops[0] = getRoot();
4127 Ops[1] = getValue(I.getOperand(1));
4128 Ops[2] = getValue(I.getOperand(2));
4129 Ops[3] = getValue(I.getOperand(3));
4127 Ops[1] = getValue(I.getOperand(0));
4128 Ops[2] = getValue(I.getOperand(1));
4129 Ops[3] = getValue(I.getOperand(2));
41304130 DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4));
41314131 return 0;
41324132 }
41354135 SDValue Ops[6];
41364136 Ops[0] = getRoot();
41374137 for (int x = 1; x < 6; ++x)
4138 Ops[x] = getValue(I.getOperand(x));
4138 Ops[x] = getValue(I.getOperand(x - 1));
41394139
41404140 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
41414141 return 0;
41444144 SDValue Root = getRoot();
41454145 SDValue L =
41464146 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
4147 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
4147 getValue(I.getOperand(1)).getValueType().getSimpleVT(),
41484148 Root,
4149 getValue(I.getOperand(0)),
41494150 getValue(I.getOperand(1)),
41504151 getValue(I.getOperand(2)),
4151 getValue(I.getOperand(3)),
4152 I.getOperand(1));
4152 I.getOperand(0));
41534153 setValue(&I, L);
41544154 DAG.setRoot(L.getValue(1));
41554155 return 0;
45194519 if (I.getNumOperands() != 4)
45204520 return false;
45214521
4522 const Value *LHS = I.getOperand(1), *RHS = I.getOperand(2);
4522 const Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
45234523 if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() ||
4524 !I.getOperand(3)->getType()->isIntegerTy() ||
4524 !I.getOperand(2)->getType()->isIntegerTy() ||
45254525 !I.getType()->isIntegerTy())
45264526 return false;
45274527
4528 const ConstantInt *Size = dyn_cast(I.getOperand(3));
4528 const ConstantInt *Size = dyn_cast(I.getOperand(2));
45294529
45304530 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
45314531 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
46164616 StringRef Name = F->getName();
46174617 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl") {
46184618 if (I.getNumOperands() == 3 && // Basic sanity checks.
4619 I.getOperand(1)->getType()->isFloatingPointTy() &&
4620 I.getType() == I.getOperand(1)->getType() &&
4621 I.getType() == I.getOperand(2)->getType()) {
4622 SDValue LHS = getValue(I.getOperand(1));
4623 SDValue RHS = getValue(I.getOperand(2));
4619 I.getOperand(0)->getType()->isFloatingPointTy() &&
4620 I.getType() == I.getOperand(0)->getType() &&
4621 I.getType() == I.getOperand(1)->getType()) {
4622 SDValue LHS = getValue(I.getOperand(0));
4623 SDValue RHS = getValue(I.getOperand(1));
46244624 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
46254625 LHS.getValueType(), LHS, RHS));
46264626 return;
46274627 }
46284628 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
46294629 if (I.getNumOperands() == 2 && // Basic sanity checks.
4630 I.getOperand(1)->getType()->isFloatingPointTy() &&
4631 I.getType() == I.getOperand(1)->getType()) {
4632 SDValue Tmp = getValue(I.getOperand(1));
4630 I.getOperand(0)->getType()->isFloatingPointTy() &&
4631 I.getType() == I.getOperand(0)->getType()) {
4632 SDValue Tmp = getValue(I.getOperand(0));
46334633 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
46344634 Tmp.getValueType(), Tmp));
46354635 return;
46364636 }
46374637 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
46384638 if (I.getNumOperands() == 2 && // Basic sanity checks.
4639 I.getOperand(1)->getType()->isFloatingPointTy() &&
4640 I.getType() == I.getOperand(1)->getType() &&
4639 I.getOperand(0)->getType()->isFloatingPointTy() &&
4640 I.getType() == I.getOperand(0)->getType() &&
46414641 I.onlyReadsMemory()) {
4642 SDValue Tmp = getValue(I.getOperand(1));
4642 SDValue Tmp = getValue(I.getOperand(0));
46434643 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
46444644 Tmp.getValueType(), Tmp));
46454645 return;
46464646 }
46474647 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
46484648 if (I.getNumOperands() == 2 && // Basic sanity checks.
4649 I.getOperand(1)->getType()->isFloatingPointTy() &&
4650 I.getType() == I.getOperand(1)->getType() &&
4649 I.getOperand(0)->getType()->isFloatingPointTy() &&
4650 I.getType() == I.getOperand(0)->getType() &&
46514651 I.onlyReadsMemory()) {
4652 SDValue Tmp = getValue(I.getOperand(1));
4652 SDValue Tmp = getValue(I.getOperand(0));
46534653 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
46544654 Tmp.getValueType(), Tmp));
46554655 return;
46564656 }
46574657 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
46584658 if (I.getNumOperands() == 2 && // Basic sanity checks.
4659 I.getOperand(1)->getType()->isFloatingPointTy() &&
4660 I.getType() == I.getOperand(1)->getType() &&
4659 I.getOperand(0)->getType()->isFloatingPointTy() &&
4660 I.getType() == I.getOperand(0)->getType() &&
46614661 I.onlyReadsMemory()) {
4662 SDValue Tmp = getValue(I.getOperand(1));
4662 SDValue Tmp = getValue(I.getOperand(0));
46634663 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
46644664 Tmp.getValueType(), Tmp));
46654665 return;
46694669 return;
46704670 }
46714671 }
4672 } else if (isa(I.getOperand(0))) {
4672 } else if (isa(I.getCalledValue())) {
46734673 visitInlineAsm(&I);
46744674 return;
46754675 }
46764676
46774677 SDValue Callee;
46784678 if (!RenameFn)
4679 Callee = getValue(I.getOperand(0));
4679 Callee = getValue(I.getCalledValue());
46804680 else
46814681 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
46824682
56085608 void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
56095609 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
56105610 MVT::Other, getRoot(),
5611 getValue(I.getOperand(1)),
5612 DAG.getSrcValue(I.getOperand(1))));
5611 getValue(I.getOperand(0)),
5612 DAG.getSrcValue(I.getOperand(0))));
56135613 }
56145614
56155615 void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
56235623 void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
56245624 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
56255625 MVT::Other, getRoot(),
5626 getValue(I.getOperand(1)),
5627 DAG.getSrcValue(I.getOperand(1))));
5626 getValue(I.getOperand(0)),
5627 DAG.getSrcValue(I.getOperand(0))));
56285628 }
56295629
56305630 void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
56315631 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
56325632 MVT::Other, getRoot(),
5633 getValue(I.getOperand(0)),
56335634 getValue(I.getOperand(1)),
5634 getValue(I.getOperand(2)),
5635 DAG.getSrcValue(I.getOperand(1)),
5636 DAG.getSrcValue(I.getOperand(2))));
5635 DAG.getSrcValue(I.getOperand(0)),
5636 DAG.getSrcValue(I.getOperand(1))));
56375637 }
56385638
56395639 /// TargetLowering::LowerCallTo - This is the default LowerCallTo
157157
158158 // Create a new invoke instruction.
159159 Args.clear();
160 Args.append(CI->op_begin() + 1, CI->op_end());
161
162 InvokeInst *II = InvokeInst::Create(CI->getOperand(0),
160 Args.append(CI->op_begin(), CI->op_end() - 1);
161
162 InvokeInst *II = InvokeInst::Create(CI->getCalledValue(),
163163 NewBB, CleanupBB,
164164 Args.begin(), Args.end(),
165165 CI->getName(), CallBB);
193193 unsigned NumMeta = 0;
194194 SmallVector Metadata;
195195 for (unsigned I = 0; I != Roots.size(); ++I) {
196 Constant *C = cast(Roots[I].first->getOperand(2));
196 Constant *C = cast(Roots[I].first->getOperand(1));
197197 if (!C->isNullValue())
198198 NumMeta = I + 1;
199199 Metadata.push_back(ConstantExpr::getBitCast(C, VoidPtr));
321321
322322 assert(Roots.empty() && "Not cleaned up?");
323323
324 SmallVectorAllocaInst*>,16> MetaRoots;
324 SmallVector AllocaInst*>,16> MetaRoots;
325325
326326 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
327327 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;)
328328 if (IntrinsicInst *CI = dyn_cast(II++))
329329 if (Function *F = CI->getCalledFunction())
330330 if (F->getIntrinsicID() == Intrinsic::gcroot) {
331 std::pair Pair = std::make_pair(
332 CI, cast(CI->getOperand(1)->stripPointerCasts()));
333 if (IsNullValue(CI->getOperand(2)))
331 std::pair Pair = std::make_pair(
332 CI, cast(CI->getOperand(0)->stripPointerCasts()));
333 if (IsNullValue(CI->getOperand(1)))
334334 Roots.push_back(Pair);
335335 else
336336 MetaRoots.push_back(Pair);
304304 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
305305 if (CallInst *CI = dyn_cast(I)) {
306306 if (CI->getCalledFunction() == SelectorFn) {
307 if (!PersonalityFn) PersonalityFn = CI->getOperand(2);
307 if (!PersonalityFn) PersonalityFn = CI->getOperand(1);
308308 EH_Selectors.push_back(CI);
309309 } else if (CI->getCalledFunction() == ExceptionFn) {
310310 EH_Exceptions.push_back(CI);
28852885 bool hasByVal = I.hasByValArgument();
28862886 bool isStructRet = I.hasStructRetAttr();
28872887 if (isStructRet) {
2888 writeOperandDeref(I.getOperand(1));
2888 writeOperandDeref(I.getOperand(0));
28892889 Out << " = ";
28902890 }
28912891
29412941
29422942 unsigned NumDeclaredParams = FTy->getNumParams();
29432943
2944 CallSite::arg_iterator AI = I.op_begin()+1, AE = I.op_end();
2944 CallInst::op_iterator AI = I.op_begin(), AE = I.op_end() - 1;
29452945 unsigned ArgNo = 0;
29462946 if (isStructRet) { // Skip struct return argument.
29472947 ++AI;
29952995 Out << "0; ";
29962996
29972997 Out << "va_start(*(va_list*)";
2998 writeOperand(I.getOperand(1));
2998 writeOperand(I.getOperand(0));
29992999 Out << ", ";
30003000 // Output the last argument to the enclosing function.
30013001 if (I.getParent()->getParent()->arg_empty())
30053005 Out << ')';
30063006 return true;
30073007 case Intrinsic::vaend:
3008 if (!isa(I.getOperand(1))) {
3008 if (!isa(I.getOperand(0))) {
30093009 Out << "0; va_end(*(va_list*)";
3010 writeOperand(I.getOperand(1));
3010 writeOperand(I.getOperand(0));
30113011 Out << ')';
30123012 } else {
30133013 Out << "va_end(*(va_list*)0)";
30163016 case Intrinsic::vacopy:
30173017 Out << "0; ";
30183018 Out << "va_copy(*(va_list*)";
3019 writeOperand(I.getOperand(0));
3020 Out << ", *(va_list*)";
30193021 writeOperand(I.getOperand(1));
3020 Out << ", *(va_list*)";
3021 writeOperand(I.getOperand(2));
30223022 Out << ')';
30233023 return true;
30243024 case Intrinsic::returnaddress:
30253025 Out << "__builtin_return_address(";
3026 writeOperand(I.getOperand(1));
3026 writeOperand(I.getOperand(0));
30273027 Out << ')';
30283028 return true;
30293029 case Intrinsic::frameaddress:
30303030 Out << "__builtin_frame_address(";
3031 writeOperand(I.getOperand(1));
3031 writeOperand(I.getOperand(0));
30323032 Out << ')';
30333033 return true;
30343034 case Intrinsic::powi:
30353035 Out << "__builtin_powi(";
3036 writeOperand(I.getOperand(0));
3037 Out << ", ";
30363038 writeOperand(I.getOperand(1));
3037 Out << ", ";
3038 writeOperand(I.getOperand(2));
30393039 Out << ')';
30403040 return true;
30413041 case Intrinsic::setjmp:
30423042 Out << "setjmp(*(jmp_buf*)";
3043 writeOperand(I.getOperand(1));
3043 writeOperand(I.getOperand(0));
30443044 Out << ')';
30453045 return true;
30463046 case Intrinsic::longjmp:
30473047 Out << "longjmp(*(jmp_buf*)";
3048 writeOperand(I.getOperand(0));
3049 Out << ", ";
30483050 writeOperand(I.getOperand(1));
3049 Out << ", ";
3050 writeOperand(I.getOperand(2));
30513051 Out << ')';
30523052 return true;
30533053 case Intrinsic::prefetch:
30543054 Out << "LLVM_PREFETCH((const void *)";
3055 writeOperand(I.getOperand(0));
3056 Out << ", ";
30553057 writeOperand(I.getOperand(1));
30563058 Out << ", ";
30573059 writeOperand(I.getOperand(2));
3058 Out << ", ";
3059 writeOperand(I.getOperand(3));
30603060 Out << ")";
30613061 return true;
30623062 case Intrinsic::stacksave:
30733073 printType(Out, I.getType());
30743074 Out << ')';
30753075 // Multiple GCC builtins multiplex onto this intrinsic.
3076 switch (cast(I.getOperand(3))->getZExtValue()) {
3076 switch (cast(I.getOperand(2))->getZExtValue()) {
30773077 default: llvm_unreachable("Invalid llvm.x86.sse.cmp!");
30783078 case 0: Out << "__builtin_ia32_cmpeq"; break;
30793079 case 1: Out << "__builtin_ia32_cmplt"; break;
30943094 Out << 'd';
30953095
30963096 Out << "(";
3097 writeOperand(I.getOperand(0));
3098 Out << ", ";
30973099 writeOperand(I.getOperand(1));
3098 Out << ", ";
3099 writeOperand(I.getOperand(2));
31003100 Out << ")";
31013101 return true;
31023102 case Intrinsic::ppc_altivec_lvsl:
31043104 printType(Out, I.getType());
31053105 Out << ')';
31063106 Out << "__builtin_altivec_lvsl(0, (void*)";
3107 writeOperand(I.getOperand(1));
3107 writeOperand(I.getOperand(0));
31083108 Out << ")";
31093109 return true;
31103110 }
32173217 DestVal = ResultVals[ValueCount].first;
32183218 DestValNo = ResultVals[ValueCount].second;
32193219 } else
3220 DestVal = CI.getOperand(ValueCount-ResultVals.size()+1);
3220 DestVal = CI.getOperand(ValueCount-ResultVals.size());
32213221
32223222 if (I->isEarlyClobber)
32233223 C = "&"+C;
32513251 }
32523252
32533253 assert(ValueCount >= ResultVals.size() && "Input can't refer to result");
3254 Value *SrcVal = CI.getOperand(ValueCount-ResultVals.size()+1);
3254 Value *SrcVal = CI.getOperand(ValueCount-ResultVals.size());
32553255
32563256 Out << "\"" << C << "\"(";
32573257 if (!I->isIndirect)
10811081
10821082 // Before we emit this instruction, we need to take care of generating any
10831083 // forward references. So, we get the names of all the operands in advance
1084 std::string* opNames = new std::string[I->getNumOperands()];
1085 for (unsigned i = 0; i < I->getNumOperands(); i++) {
1084 const unsigned Ops(I->getNumOperands());
1085 std::string* opNames = new std::string[Ops];
1086 for (unsigned i = 0; i < Ops; i++) {
10861087 opNames[i] = getOpName(I->getOperand(i));
10871088 }
10881089
11431144 const InvokeInst* inv = cast(I);
11441145 Out << "std::vector " << iName << "_params;";
11451146 nl(Out);
1146 for (unsigned i = 3; i < inv->getNumOperands(); ++i) {
1147 for (unsigned i = 0; i < inv->getNumOperands() - 3; ++i) {
11471148 Out << iName << "_params.push_back("
11481149 << opNames[i] << ");";
11491150 nl(Out);
11501151 }
11511152 Out << "InvokeInst *" << iName << " = InvokeInst::Create("
1152 << opNames[0] << ", "
1153 << opNames[1] << ", "
1154 << opNames[2] << ", "
1153 << opNames[Ops - 3] << ", "
1154 << opNames[Ops - 2] << ", "
1155 << opNames[Ops - 1] << ", "
11551156 << iName << "_params.begin(), " << iName << "_params.end(), \"";
11561157 printEscapedString(inv->getName());
11571158 Out << "\", " << bbname << ");";
13871388 if (call->getNumOperands() > 2) {
13881389 Out << "std::vector " << iName << "_params;";
13891390 nl(Out);
1390 for (unsigned i = 1; i < call->getNumOperands(); ++i) {
1391 for (unsigned i = 0; i < call->getNumOperands() - 1; ++i) {
13911392 Out << iName << "_params.push_back(" << opNames[i] << ");";
13921393 nl(Out);
13931394 }
13941395 Out << "CallInst* " << iName << " = CallInst::Create("
1395 << opNames[0] << ", " << iName << "_params.begin(), "
1396 << opNames[Ops - 1] << ", " << iName << "_params.begin(), "
13961397 << iName << "_params.end(), \"";
13971398 } else if (call->getNumOperands() == 2) {
13981399 Out << "CallInst* " << iName << " = CallInst::Create("
1399 << opNames[0] << ", " << opNames[1] << ", \"";
1400 << opNames[Ops - 1] << ", " << opNames[0] << ", \"";
14001401 } else {
1401 Out << "CallInst* " << iName << " = CallInst::Create(" << opNames[0]
1402 Out << "CallInst* " << iName << " = CallInst::Create(" << opNames[Ops - 1]
14021403 << ", \"";
14031404 }
14041405 printEscapedString(call->getName());
11691169 // Emit code inline code to store the stack guard onto the stack.
11701170 EVT PtrTy = TLI.getPointerTy();
11711171
1172 const Value *Op1 = I.getOperand(1); // The guard's value.
1173 const AllocaInst *Slot = cast(I.getOperand(2));
1172 const Value *Op1 = I.getOperand(0); // The guard's value.
1173 const AllocaInst *Slot = cast(I.getOperand(1));
11741174
11751175 // Grab the frame index.
11761176 X86AddressMode AM;
11811181 return true;
11821182 }
11831183 case Intrinsic::objectsize: {
1184 ConstantInt *CI = dyn_cast(I.getOperand(2));
1184 ConstantInt *CI = dyn_cast(I.getOperand(1));
11851185 const Type *Ty = I.getCalledFunction()->getReturnType();
11861186
11871187 assert(CI && "Non-constant type in Intrinsic::objectsize?");
12361236 if (!isTypeLegal(RetTy, VT))
12371237 return false;
12381238
1239 const Value *Op1 = I.getOperand(1);
1240 const Value *Op2 = I.getOperand(2);
1239 const Value *Op1 = I.getOperand(0);
1240 const Value *Op2 = I.getOperand(1);
12411241 unsigned Reg1 = getRegForValue(Op1);
12421242 unsigned Reg2 = getRegForValue(Op2);
12431243
12801280
12811281 bool X86FastISel::X86SelectCall(const Instruction *I) {
12821282 const CallInst *CI = cast(I);
1283 const Value *Callee = I->getOperand(0);
1283 const Value *Callee = CI->getCalledValue();
12841284
12851285 // Can't handle inline asm yet.
12861286 if (isa(Callee))
99589958
99599959 // Verify this is a simple bswap.
99609960 if (CI->getNumOperands() != 2 ||
9961 CI->getType() != CI->getOperand(1)->getType() ||
9961 CI->getType() != CI->getOperand(0)->getType() ||
99629962 !CI->getType()->isIntegerTy())
99639963 return false;
99649964
99719971 Module *M = CI->getParent()->getParent()->getParent();
99729972 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
99739973
9974 Value *Op = CI->getOperand(1);
9974 Value *Op = CI->getOperand(0);
99759975 Op = CallInst::Create(Int, Op, CI->getName(), CI);
99769976
99779977 CI->replaceAllUsesWith(Op);
221221 GS.HasPHIUser = true;
222222 } else if (isa(I)) {
223223 } else if (isa(I)) {
224 if (I->getOperand(0) == V)
225 GS.StoredType = GlobalStatus::isStored;
224226 if (I->getOperand(1) == V)
225 GS.StoredType = GlobalStatus::isStored;
226 if (I->getOperand(2) == V)
227227 GS.isLoaded = true;
228228 } else if (isa(I)) {
229 assert(I->getOperand(1) == V && "Memset only takes one pointer!");
229 assert(I->getOperand(0) == V && "Memset only takes one pointer!");
230230 GS.StoredType = GlobalStatus::isStored;
231231 } else {
232232 return true; // Any other non-load instruction might take address!
13221322 // if (F2) { free(F2); F2 = 0; }
13231323 // }
13241324 // The malloc can also fail if its argument is too large.
1325 Constant *ConstantZero = ConstantInt::get(CI->getOperand(1)->getType(), 0);
1326 Value *RunningOr = new ICmpInst(CI, ICmpInst::ICMP_SLT, CI->getOperand(1),
1325 Constant *ConstantZero = ConstantInt::get(CI->getOperand(0)->getType(), 0);
1326 Value *RunningOr = new ICmpInst(CI, ICmpInst::ICMP_SLT, CI->getOperand(0),
13271327 ConstantZero, "isneg");
13281328 for (unsigned i = 0, e = FieldMallocs.size(); i != e; ++i) {
13291329 Value *Cond = new ICmpInst(CI, ICmpInst::ICMP_EQ, FieldMallocs[i],
15101510
15111511 // If this is an allocation of a fixed size array of structs, analyze as a
15121512 // variable size array. malloc [100 x struct],1 -> malloc struct, 100
1513 if (NElems == ConstantInt::get(CI->getOperand(1)->getType(), 1))
1513 if (NElems == ConstantInt::get(CI->getOperand(0)->getType(), 1))
15141514 if (const ArrayType *AT = dyn_cast(AllocTy))
15151515 AllocTy = AT->getElementType();
1516
1516
15171517 const StructType *AllocSTy = dyn_cast(AllocTy);
15181518 if (!AllocSTy)
15191519 return false;
16401640 // bool.
16411641 Instruction *StoredVal = cast(SI->getOperand(0));
16421642
1643 // If we're already replaced the input, StoredVal will be a cast or
1643 // If we've already replaced the input, StoredVal will be a cast or
16441644 // select instruction. If not, it will be a load of the original
16451645 // global.
16461646 if (LoadInst *LI = dyn_cast(StoredVal)) {
22612261 } else if (SelectInst *SI = dyn_cast(CurInst)) {
22622262 InstResult =
22632263 ConstantExpr::getSelect(getVal(Values, SI->getOperand(0)),
2264 getVal(Values, SI->getOperand(1)),
2265 getVal(Values, SI->getOperand(2)));
2264 getVal(Values, SI->getOperand(1)),
2265 getVal(Values, SI->getOperand(2)));
22662266 } else if (GetElementPtrInst *GEP = dyn_cast(CurInst)) {
22672267 Constant *P = getVal(Values, GEP->getOperand(0));
22682268 SmallVector GEPOps;
22942294 }
22952295
22962296 // Cannot handle inline asm.
2297 if (isa(CI->getOperand(0))) return false;
2297 if (isa(CI->getCalledValue())) return false;
22982298
22992299 // Resolve function pointers.
2300 Function *Callee = dyn_cast(getVal(Values, CI->getOperand(0)));
2300 Function *Callee = dyn_cast(getVal(Values, CI->getCalledValue()));
23012301 if (!Callee) return false; // Cannot resolve.
23022302
23032303 SmallVector Formals;
2304 for (User::op_iterator i = CI->op_begin() + 1, e = CI->op_end();
2304 for (User::op_iterator i = CI->op_begin(), e = CI->op_end() - 1;
23052305 i != e; ++i)
23062306 Formals.push_back(getVal(Values, *i));
23072307
261261 // char*. It returns "void", so it doesn't need to replace any of
262262 // Inst's uses and doesn't get a name.
263263 CastInst* CI =
264 new BitCastInst(Inst->getOperand(1), SBPTy, "LJBuf", Inst);
265 Value *Args[] = { CI, Inst->getOperand(2) };
264 new BitCastInst(Inst->getOperand(0), SBPTy, "LJBuf", Inst);
265 Value *Args[] = { CI, Inst->getOperand(1) };
266266 CallInst::Create(ThrowLongJmp, Args, Args + 2, "", Inst);
267267
268268 SwitchValuePair& SVP = SwitchValMap[Inst->getParent()->getParent()];
377377 const Type* SBPTy =
378378 Type::getInt8PtrTy(Inst->getContext());
379379 CastInst* BufPtr =
380 new BitCastInst(Inst->getOperand(1), SBPTy, "SBJmpBuf", Inst);
380 new BitCastInst(Inst->getOperand(0), SBPTy, "SBJmpBuf", Inst);
381381 Value *Args[] = {
382382 GetSetJmpMap(Func), BufPtr,
383383 ConstantInt::get(Type::getInt32Ty(Inst->getContext()), SetJmpIDMap[Func]++)
472472
473473 // Construct the new "invoke" instruction.
474474 TerminatorInst* Term = OldBB->getTerminator();
475 std::vector Params(CI.op_begin() + 1, CI.op_end());
475 std::vector Params(CI.op_begin(), CI.op_end() - 1);
476476 InvokeInst* II =
477477 InvokeInst::Create(CI.getCalledValue(), NewBB, PrelimBBMap[Func],
478478 Params.begin(), Params.end(), CI.getName(), Term);
108108 }
109109
110110 Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) {
111 unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(1));
112 unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(2));
111 unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(0));
112 unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(1));
113113 unsigned MinAlign = std::min(DstAlign, SrcAlign);
114114 unsigned CopyAlign = MI->getAlignment();
115115
121121
122122 // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with
123123 // load/store.
124 ConstantInt *MemOpLength = dyn_cast(MI->getOperand(3));
124 ConstantInt *MemOpLength = dyn_cast(MI->getOperand(2));
125125 if (MemOpLength == 0) return 0;
126126
127127 // Source and destination pointer types are always "i8*" for intrinsic. See
136136
137137 // Use an integer load+store unless we can find something better.
138138 unsigned SrcAddrSp =
139 cast(MI->getOperand(2)->getType())->getAddressSpace();
139 cast(MI->getOperand(1)->getType())->getAddressSpace();
140140 unsigned DstAddrSp =
141 cast(MI->getOperand(1)->getType())->getAddressSpace();
141 cast(MI->getOperand(0)->getType())->getAddressSpace();
142142
143143 const IntegerType* IntType = IntegerType::get(MI->getContext(), Size<<3);
144144 Type *NewSrcPtrTy = PointerType::get(IntType, SrcAddrSp);
150150 // an i64 load+store, here because this improves the odds that the source or
151151 // dest address will be promotable. See if we can find a better type than the
152152 // integer datatype.
153 Value *StrippedDest = MI->getOperand(1)->stripPointerCasts();
154 if (StrippedDest != MI->getOperand(1)) {
153 Value *StrippedDest = MI->getOperand(0)->stripPointerCasts();
154 if (StrippedDest != MI->getOperand(0)) {
155155 const Type *SrcETy = cast(StrippedDest->getType())
156156 ->getElementType();
157157 if (TD && SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) {
185185 SrcAlign = std::max(SrcAlign, CopyAlign);
186186 DstAlign = std::max(DstAlign, CopyAlign);
187187
188 Value *Src = Builder->CreateBitCast(MI->getOperand(2), NewSrcPtrTy);
189 Value *Dest = Builder->CreateBitCast(MI->getOperand(1), NewDstPtrTy);
188 Value *Src = Builder->CreateBitCast(MI->getOperand(1), NewSrcPtrTy);
189 Value *Dest = Builder->CreateBitCast(MI->getOperand(0), NewDstPtrTy);
190190 Instruction *L = new LoadInst(Src, "tmp", MI->isVolatile(), SrcAlign);
191191 InsertNewInstBefore(L, *MI);
192192 InsertNewInstBefore(new StoreInst(L, Dest, MI->isVolatile(), DstAlign),
193193 *MI);
194194
195195 // Set the size of the copy to 0, it will be deleted on the next iteration.
196 MI->setOperand(3, Constant::getNullValue(MemOpLength->getType()));
196 MI->setOperand(2, Constant::getNullValue(MemOpLength->getType()));
197197 return MI;
198198 }
199199
257257
258258 IntrinsicInst *II = dyn_cast(&CI);
259259 if (!II) return visitCallSite(&CI);
260
260
261261 // Intrinsics cannot occur in an invoke, so handle them here instead of in
262262 // visitCallSite.
263263 if (MemIntrinsic *MI = dyn_cast(II)) {
281281 if (MemMoveInst *MMI = dyn_cast(MI)) {
282282 if (GlobalVariable *GVSrc = dyn_cast(MMI->getSource()))
283283 if (GVSrc->isConstant()) {
284 Module *M = CI.getParent()->getParent()->getParent();
284 Module *M = MMI->getParent()->getParent()->getParent();
285285 Intrinsic::ID MemCpyID = Intrinsic::memcpy;
286 const Type *Tys[3] = { CI.getOperand(1)->getType(),
287 CI.getOperand(2)->getType(),
288 CI.getOperand(3)->getType() };
289 CI.setOperand(0,
286 const Type *Tys[3] = { CI.getOperand(0)->getType(),
287 CI.getOperand(1)->getType(),
288 CI.getOperand(2)->getType() };
289 MMI->setCalledFunction(
290290 Intrinsic::getDeclaration(M, MemCpyID, Tys, 3));
291291 Changed = true;
292292 }
296296 // memmove(x,x,size) -> noop.
297297 if (MTI->getSource() == MTI->getDest())
298298 return EraseInstFromFunction(CI);
299 }
300
301 // If we can determine a pointer alignment that is bigger than currently
302 // set, update the alignment.
303 if (isa(MI)) {
304 if (Instruction *I = SimplifyMemTransfer(MI))
299
300 // If we can determine a pointer alignment that is bigger than currently
301 // set, update the alignment.
302 if (Instruction *I = SimplifyMemTransfer(MTI))
305303 return I;
306304 } else if (MemSetInst *MSI = dyn_cast(MI)) {
307305 if (Instruction *I = SimplifyMemSet(MSI))
308306 return I;
309307 }
310
308
311309 if (Changed) return II;
312310 }
313
311
314312 switch (II->getIntrinsicID()) {
315313 default: break;
316314 case Intrinsic::objectsize: {
318316 if (!TD) break;
319317
320318 const Type *ReturnTy = CI.getType();
321 bool Min = (cast(II->getOperand(2))->getZExtValue() == 1);
319 bool Min = (cast(II->getOperand(1))->getZExtValue() == 1);
322320
323321 // Get to the real allocated thing and offset as fast as possible.
324 Value *Op1 = II->getOperand(1)->stripPointerCasts();
322 Value *Op1 = II->getOperand(0)->stripPointerCasts();
325323
326324 // If we've stripped down to a single global variable that we
327325 // can know the size of then just return that.
389387
390388 Constant *RetVal = ConstantInt::get(ReturnTy, Size-Offset);
391389 return ReplaceInstUsesWith(CI, RetVal);
392
393390 }
394391
395392 // Do not return "I don't know" here. Later optimization passes could
398395 }
399396 case Intrinsic::bswap:
400397 // bswap(bswap(x)) -> x
401 if (IntrinsicInst *Operand = dyn_cast(II->getOperand(1)))
398 if (IntrinsicInst *Operand = dyn_cast(II->getOperand(0)))
402399 if (Operand->getIntrinsicID() == Intrinsic::bswap)
403 return ReplaceInstUsesWith(CI, Operand->getOperand(1));
400 return ReplaceInstUsesWith(CI, Operand->getOperand(0));
404401
405402 // bswap(trunc(bswap(x))) -> trunc(lshr(x, c))
406 if (TruncInst *TI = dyn_cast(II->getOperand(1))) {
403 if (TruncInst *TI = dyn_cast(II->getOperand(0))) {
407404 if (IntrinsicInst *Operand = dyn_cast(TI->getOperand(0)))
408405 if (Operand->getIntrinsicID() == Intrinsic::bswap) {
409406 unsigned C = Operand->getType()->getPrimitiveSizeInBits() -
410407 TI->getType()->getPrimitiveSizeInBits();
411408 Value *CV = ConstantInt::get(Operand->getType(), C);
412 Value *V = Builder->CreateLShr(Operand->getOperand(1), CV);
409 Value *V = Builder->CreateLShr(Operand->getOperand(0), CV);
413410 return new TruncInst(V, TI->getType());
414411 }
415412 }
416413
417414 break;
418415 case Intrinsic::powi:
419 if (ConstantInt *Power = dyn_cast(II->getOperand(2))) {
416 if (ConstantInt *Power = dyn_cast(II->getOperand(1))) {
420417 // powi(x, 0) -> 1.0
421418 if (Power->isZero())
422419 return ReplaceInstUsesWith(CI, ConstantFP::get(CI.getType(), 1.0));
423420 // powi(x, 1) -> x
424421 if (Power->isOne())
425 return ReplaceInstUsesWith(CI, II->getOperand(1));
422 return ReplaceInstUsesWith(CI, II->getOperand(0));
426423 // powi(x, -1) -> 1/x
427424 if (Power->isAllOnesValue())
428425 return BinaryOperator::CreateFDiv(ConstantFP::get(CI.getType(), 1.0),
429 II->getOperand(1));
426 II->getOperand(0));
430427 }
431428 break;
432429 case Intrinsic::cttz: {
433430 // If all bits below the first known one are known zero,
434431 // this value is constant.
435 const IntegerType *IT = cast(II->getOperand(1)->getType());
432 const IntegerType *IT = cast(II->getOperand(0)->getType());
436433 uint32_t BitWidth = IT->getBitWidth();
437434 APInt KnownZero(BitWidth, 0);
438435 APInt KnownOne(BitWidth, 0);
439 ComputeMaskedBits(II->getOperand(1), APInt::getAllOnesValue(BitWidth),
436 ComputeMaskedBits(II->getOperand(0), APInt::getAllOnesValue(BitWidth),
440437 KnownZero, KnownOne);
441438 unsigned TrailingZeros = KnownOne.countTrailingZeros();
442439 APInt Mask(APInt::getLowBitsSet(BitWidth, TrailingZeros));
449446 case Intrinsic::ctlz: {
450447 // If all bits above the first known one are known zero,
451448 // this value is constant.
452 const IntegerType *IT = cast(II->getOperand(1)->getType());
449 const IntegerType *IT = cast(II->getOperand(0)->getType());
453450 uint32_t BitWidth = IT->getBitWidth();
454451 APInt KnownZero(BitWidth, 0);
455452 APInt KnownOne(BitWidth, 0);
456 ComputeMaskedBits(II->getOperand(1), APInt::getAllOnesValue(BitWidth),
453 ComputeMaskedBits(II->getOperand(0), APInt::getAllOnesValue(BitWidth),
457454 KnownZero, KnownOne);
458455 unsigned LeadingZeros = KnownOne.countLeadingZeros();
459456 APInt Mask(APInt::getHighBitsSet(BitWidth, LeadingZeros));
464461 }
465462 break;
466463 case Intrinsic::uadd_with_overflow: {
467 Value *LHS = II->getOperand(1), *RHS = II->getOperand(2);
468 const IntegerType *IT = cast(II->getOperand(1)->getType());
464 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
465 const IntegerType *IT = cast(II->getOperand(0)->getType());
469466 uint32_t BitWidth = IT->getBitWidth();
470467 APInt Mask = APInt::getSignBit(BitWidth);
471468 APInt LHSKnownZero(BitWidth, 0);
509506 // FALL THROUGH uadd into sadd
510507 case Intrinsic::sadd_with_overflow:
511508 // Canonicalize constants into the RHS.
512 if (isa(II->getOperand(1)) &&
513 !isa(II->getOperand(2))) {
514 Value *LHS = II->getOperand(1);
515 II->setOperand(1, II->getOperand(2));
516 II->setOperand(2, LHS);
509 if (isa(II->getOperand(0)) &&
510 !isa(II->getOperand(1))) {
511 Value *LHS = II->getOperand(0);
512 II->setOperand(0, II->getOperand(1));
513 II->setOperand(1, LHS);
517514 return II;
518515 }
519516
520517 // X + undef -> undef
521 if (isa(II->getOperand(2)))
518 if (isa(II->getOperand(1)))
522519 return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
523520
524 if (ConstantInt *RHS = dyn_cast(II->getOperand(2))) {
521 if (ConstantInt *RHS = dyn_cast(II->getOperand(1))) {
525522 // X + 0 -> {X, false}
526523 if (RHS->isZero()) {
527524 Constant *V[] = {
529526 ConstantInt::getFalse(II->getContext())
530527 };
531528 Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
532 return InsertValueInst::Create(Struct, II->getOperand(1), 0);
529 return InsertValueInst::Create(Struct, II->getOperand(0), 0);
533530 }
534531 }
535532 break;
537534 case Intrinsic::ssub_with_overflow:
538535 // undef - X -> undef
539536 // X - undef -> undef
540 if (isa(II->getOperand(1)) ||
541 isa(II->getOperand(2)))
537 if (isa(II->getOperand(0)) ||
538 isa(II->getOperand(1)))
542539 return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
543540
544 if (ConstantInt *RHS = dyn_cast(II->getOperand(2))) {
541 if (ConstantInt *RHS = dyn_cast(II->getOperand(1))) {
545542 // X - 0 -> {X, false}
546543 if (RHS->isZero()) {
547544 Constant *V[] = {
548 UndefValue::get(II->getOperand(1)->getType()),
545 UndefValue::get(II->getOperand(0)->getType()),
549546 ConstantInt::getFalse(II->getContext())
550547 };
551548 Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
552 return InsertValueInst::Create(Struct, II->getOperand(1), 0);
549 return InsertValueInst::Create(Struct, II->getOperand(0), 0);
553550 }
554551 }
555552 break;
556553 case Intrinsic::umul_with_overflow:
557554 case Intrinsic::smul_with_overflow:
558555 // Canonicalize constants into the RHS.
559 if (isa(II->getOperand(1)) &&
560 !isa(II->getOperand(2))) {
561 Value *LHS = II->getOperand(1);
562 II->setOperand(1, II->getOperand(2));
563 II->setOperand(2, LHS);
556 if (isa(II->getOperand(0)) &&
557 !isa(II->getOperand(1))) {
558 Value *LHS = II->getOperand(0);
559 II->setOperand(0, II->getOperand(1));
560 II->setOperand(1, LHS);
564561 return II;
565562 }
566563
567564 // X * undef -> undef
568 if (isa(II->getOperand(2)))
565 if (isa(II->getOperand(1)))
569566 return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
570567
571 if (ConstantInt *RHSI = dyn_cast(II->getOperand(2))) {
568 if (ConstantInt *RHSI = dyn_cast(II->getOperand(1))) {
572569 // X*0 -> {0, false}
573570 if (RHSI->isZero())
574571 return ReplaceInstUsesWith(CI, Constant::getNullValue(II->getType()));
576573 // X * 1 -> {X, false}
577574 if (RHSI->equalsInt(1)) {
578575 Constant *V[] = {
579 UndefValue::get(II->getOperand(1)->getType()),
576 UndefValue::get(II->getOperand(0)->getType()),
580577 ConstantInt::getFalse(II->getContext())
581578 };
582579 Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
583 return InsertValueInst::Create(Struct, II->getOperand(1), 0);
580 return InsertValueInst::Create(Struct, II->getOperand(0), 0);
584581 }
585582 }
586583 break;
591588 case Intrinsic::x86_sse2_loadu_dq:
592589 // Turn PPC lvx -> load if the pointer is known aligned.
593590 // Turn X86 loadups -> load if the pointer is known aligned.
594 if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) {
595 Value *Ptr = Builder->CreateBitCast(II->getOperand(1),
591 if (GetOrEnforceKnownAlignment(II->getOperand(0), 16) >= 16) {
592 Value *Ptr = Builder->CreateBitCast(II->getOperand(0),
596593 PointerType::getUnqual(II->getType()));
597594 return new LoadInst(Ptr);
598595 }
600597 case Intrinsic::ppc_altivec_stvx:
601598 case Intrinsic::ppc_altivec_stvxl:
602599 // Turn stvx -> store if the pointer is known aligned.
603 if (GetOrEnforceKnownAlignment(II->getOperand(2), 16) >= 16) {
600 if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) {
604601 const Type *OpPtrTy =
605 PointerType::getUnqual(II->getOperand(1)->getType());
606 Value *Ptr = Builder->CreateBitCast(II->getOperand(2), OpPtrTy);
607 return new StoreInst(II->getOperand(1), Ptr);
602 PointerType::getUnqual(II->getOperand(0)->getType());
603 Value *Ptr = Builder->CreateBitCast(II->getOperand(1), OpPtrTy);
604 return new StoreInst(II->getOperand(0), Ptr);
608605 }
609606 break;
610607 case Intrinsic::x86_sse_storeu_ps:
611608 case Intrinsic::x86_sse2_storeu_pd:
612609 case Intrinsic::x86_sse2_storeu_dq:
613610 // Turn X86 storeu -> store if the pointer is known aligned.
614 if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) {
611 if (GetOrEnforceKnownAlignment(II->getOperand(0), 16) >= 16) {
615612 const Type *OpPtrTy =
616 PointerType::getUnqual(II->getOperand(2)->getType());
617 Value *Ptr = Builder->CreateBitCast(II->getOperand(1), OpPtrTy);
618 return new StoreInst(II->getOperand(2), Ptr);
613 PointerType::getUnqual(II->getOperand(1)->getType());
614 Value *Ptr = Builder->CreateBitCast(II->getOperand(0), OpPtrTy);
615 return new StoreInst(II->getOperand(1), Ptr);
619616 }
620617 break;
621618
623620 // These intrinsics only demands the 0th element of its input vector. If
624621 // we can simplify the input based on that, do so now.
625622 unsigned VWidth =
626 cast(II->getOperand(1)->getType())->getNumElements();
623 cast(II->getOperand(0)->getType())->getNumElements();
627624 APInt DemandedElts(VWidth, 1);
628625 APInt UndefElts(VWidth, 0);
629 if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts,
626 if (Value *V = SimplifyDemandedVectorElts(II->getOperand(0), DemandedElts,
630627 UndefElts)) {
631 II->setOperand(1, V);
628 II->setOperand(0, V);
632629 return II;
633630 }
634631 break;
636633
637634 case Intrinsic::ppc_altivec_vperm:
638635 // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant.
639 if (ConstantVector *Mask = dyn_cast(II->getOperand(3))) {
636 if (ConstantVector *Mask = dyn_cast(II->getOperand(2))) {
640637 assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!");
641638
642639 // Check that all of the elements are integer constants or undefs.
651648
652649 if (AllEltsOk) {
653650 // Cast the input vectors to byte vectors.
654 Value *Op0 = Builder->CreateBitCast(II->getOperand(1), Mask->getType());
655 Value *Op1 = Builder->CreateBitCast(II->getOperand(2), Mask->getType());
651 Value *Op0 = Builder->CreateBitCast(II->getOperand(0), Mask->getType());
652 Value *Op1 = Builder->CreateBitCast(II->getOperand(1), Mask->getType());
656653 Value *Result = UndefValue::get(Op0->getType());
657654
658655 // Only extract each element once.
685682 case Intrinsic::stackrestore: {
686683 // If the save is right next to the restore, remove the restore. This can
687684 // happen when variable allocas are DCE'd.
688 if (IntrinsicInst *SS = dyn_cast(II->getOperand(1))) {
685 if (IntrinsicInst *SS = dyn_cast(II->getOperand(0))) {
689686 if (SS->getIntrinsicID() == Intrinsic::stacksave) {
690687 BasicBlock::iterator BI = SS;
691688 if (&*++BI == II)
842839 UndefValue::get(Type::getInt1PtrTy(Callee->getContext())),
843840 CS.getInstruction());
844841
845 // If CS dues not return void then replaceAllUsesWith undef.
842 // If CS does not return void then replaceAllUsesWith undef.
846843 // This allows ValueHandlers and custom metadata to adjust itself.
847844 if (!CS.getInstruction()->getType()->isVoidTy())
848845 CS.getInstruction()->
11361133 IntrinsicInst *Tramp =
11371134 cast(cast(Callee)->getOperand(0));
11381135
1139 Function *NestF = cast(Tramp->getOperand(2)->stripPointerCasts());
1136 Function *NestF = cast(Tramp->getOperand(1)->stripPointerCasts());
11401137 const PointerType *NestFPTy = cast(NestF->getType());
11411138 const FunctionType *NestFTy = cast(NestFPTy->getElementType());
11421139
11771174 do {
11781175 if (Idx == NestIdx) {
11791176 // Add the chain argument and attributes.
1180 Value *NestVal = Tramp->getOperand(3);
1177 Value *NestVal = Tramp->getOperand(2);
11811178 if (NestVal->getType() != NestTy)
11821179 NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller);
11831180 NewArgs.push_back(NestVal);
14221422 switch (II->getIntrinsicID()) {
14231423 case Intrinsic::bswap:
14241424 Worklist.Add(II);
1425 ICI.setOperand(0, II->getOperand(1));
1425 ICI.setOperand(0, II->getOperand(0));
14261426 ICI.setOperand(1, ConstantInt::get(II->getContext(), RHSV.byteSwap()));
14271427 return &ICI;
14281428 case Intrinsic::ctlz:
14301430 // ctz(A) == bitwidth(a) -> A == 0 and likewise for !=
14311431 if (RHSV == RHS->getType()->getBitWidth()) {
14321432 Worklist.Add(II);
1433 ICI.setOperand(0, II->getOperand(1));
1433 ICI.setOperand(0, II->getOperand(0));
14341434 ICI.setOperand(1, ConstantInt::get(RHS->getType(), 0));
14351435 return &ICI;
14361436 }
14391439 // popcount(A) == 0 -> A == 0 and likewise for !=
14401440 if (RHS->isZero()) {
14411441 Worklist.Add(II);
1442 ICI.setOperand(0, II->getOperand(1));
1442 ICI.setOperand(0, II->getOperand(0));
14431443 ICI.setOperand(1, RHS);
14441444 return &ICI;
14451445 }
403403 isPowerOf2_32(BitWidth) && Log2_32(BitWidth) == Op1C->getZExtValue()){
404404 bool isCtPop = II->getIntrinsicID() == Intrinsic::ctpop;
405405 Constant *RHS = ConstantInt::getSigned(Op0->getType(), isCtPop ? -1:0);
406 Value *Cmp = Builder->CreateICmpEQ(II->getOperand(1), RHS);
406 Value *Cmp = Builder->CreateICmpEQ(II->getOperand(0), RHS);
407407 return new ZExtInst(Cmp, II->getType());
408408 }
409409 }
731731 // the right place.
732732 Instruction *NewVal;
733733 if (InputBit > ResultBit)
734 NewVal = BinaryOperator::CreateLShr(I->getOperand(1),
734 NewVal = BinaryOperator::CreateLShr(II->getOperand(0),
735735 ConstantInt::get(I->getType(), InputBit-ResultBit));
736736 else
737 NewVal = BinaryOperator::CreateShl(I->getOperand(1),
737 NewVal = BinaryOperator::CreateShl(II->getOperand(0),
738738 ConstantInt::get(I->getType(), ResultBit-InputBit));
739739 NewVal->takeName(I);
740740 return InsertNewInstBefore(NewVal, *I);
10511051 case Intrinsic::x86_sse2_mul_sd:
10521052 case Intrinsic::x86_sse2_min_sd:
10531053 case Intrinsic::x86_sse2_max_sd:
1054 TmpV = SimplifyDemandedVectorElts(II->getOperand(0), DemandedElts,
1055 UndefElts, Depth+1);
1056 if (TmpV) { II->setOperand(0, TmpV); MadeChange = true; }
10541057 TmpV = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts,
1055 UndefElts, Depth+1);
1058 UndefElts2, Depth+1);
10561059 if (TmpV) { II->setOperand(1, TmpV); MadeChange = true; }
1057 TmpV = SimplifyDemandedVectorElts(II->getOperand(2), DemandedElts,
1058 UndefElts2, Depth+1);
1059 if (TmpV) { II->setOperand(2, TmpV); MadeChange = true; }
10601060
10611061 // If only the low elt is demanded and this is a scalarizable intrinsic,
10621062 // scalarize it now.
10681068 case Intrinsic::x86_sse2_sub_sd:
10691069 case Intrinsic::x86_sse2_mul_sd:
10701070 // TODO: Lower MIN/MAX/ABS/etc
1071 Value *LHS = II->getOperand(1);
1072 Value *RHS = II->getOperand(2);
1071 Value *LHS = II->getOperand(0);
1072 Value *RHS = II->getOperand(1);
10731073 // Extract the element as scalars.
10741074 LHS = InsertNewInstBefore(ExtractElementInst::Create(LHS,
10751075 ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U)), *II);
710710 }
711711
712712 Instruction *InstCombiner::visitFree(Instruction &FI) {
713 Value *Op = FI.getOperand(1);
713 Value *Op = FI.getOperand(0);
714714
715715 // free undef -> unreachable.
716716 if (isa(Op)) {
895895 if (IntrinsicInst *II = dyn_cast(Agg)) {
896896 // We're extracting from an intrinsic, see if we're the only user, which
897897 // allows us to simplify multiple result intrinsics to simpler things that
898 // just get one value..
898 // just get one value.
899899 if (II->hasOneUse()) {
900900 // Check if we're grabbing the overflow bit or the result of a 'with
901901 // overflow' intrinsic. If it's the latter we can remove the intrinsic
904904 case Intrinsic::uadd_with_overflow:
905905 case Intrinsic::sadd_with_overflow:
906906 if (*EV.idx_begin() == 0) { // Normal result.
907 Value *LHS = II->getOperand(1), *RHS = II->getOperand(2);
907 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
908908 II->replaceAllUsesWith(UndefValue::get(II->getType()));
909909 EraseInstFromFunction(*II);
910910 return BinaryOperator::CreateAdd(LHS, RHS);
913913 case Intrinsic::usub_with_overflow:
914914 case Intrinsic::ssub_with_overflow:
915915 if (*EV.idx_begin() == 0) { // Normal result.
916 Value *LHS = II->getOperand(1), *RHS = II->getOperand(2);
916 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
917917 II->replaceAllUsesWith(UndefValue::get(II->getType()));
918918 EraseInstFromFunction(*II);
919919 return BinaryOperator::CreateSub(LHS, RHS);
922922 case Intrinsic::umul_with_overflow:
923923 case Intrinsic::smul_with_overflow:
924924 if (*EV.idx_begin() == 0) { // Normal result.
925 Value *LHS = II->getOperand(1), *RHS = II->getOperand(2);
925 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
926926 II->replaceAllUsesWith(UndefValue::get(II->getType()));
927927 EraseInstFromFunction(*II);
928928 return BinaryOperator::CreateMul(LHS, RHS);
7272 if (AI->getType() != ArgVTy) {
7373 Instruction::CastOps opcode = CastInst::getCastOpcode(AI, false, ArgVTy,
7474 false);
75 InitCall->setOperand(2,
75 InitCall->setOperand(1,
7676 CastInst::Create(opcode, AI, ArgVTy, "argv.cast", InitCall));
7777 } else {
78 InitCall->setOperand(2, AI);
78 InitCall->setOperand(1, AI);
7979 }
8080 /* FALL THROUGH */
8181
9292 }
9393 opcode = CastInst::getCastOpcode(AI, true,
9494 Type::getInt32Ty(Context), true);
95 InitCall->setOperand(1,
95 InitCall->setOperand(0,
9696 CastInst::Create(opcode, AI, Type::getInt32Ty(Context),
9797 "argc.cast", InitCall));
9898 } else {
9999 AI->replaceAllUsesWith(InitCall);
100 InitCall->setOperand(1, AI);
100 InitCall->setOperand(0, AI);
101101 }
102102
103103 case 0: break;
558558 // Lower all uses of llvm.objectsize.*
559559 IntrinsicInst *II = dyn_cast(CI);
560560 if (II && II->getIntrinsicID() == Intrinsic::objectsize) {
561 bool Min = (cast(II->getOperand(2))->getZExtValue() == 1);
561 bool Min = (cast(II->getOperand(1))->getZExtValue() == 1);
562562 const Type *ReturnTy = CI->getType();
563563 Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL);
564564 CI->replaceAllUsesWith(RetVal);
122122 if (StoreInst *SI = dyn_cast(I))
123123 return SI->getPointerOperand();
124124 if (MemIntrinsic *MI = dyn_cast(I))
125 return MI->getOperand(1);
125 return MI->getOperand(0);
126126
127127 switch (cast(I)->getIntrinsicID()) {
128128 default: assert(false && "Unexpected intrinsic!");
129129 case Intrinsic::init_trampoline:
130 return I->getOperand(0);
131 case Intrinsic::lifetime_end:
130132 return I->getOperand(1);
131 case Intrinsic::lifetime_end:
132 return I->getOperand(2);
133133 }
134134 }
135135
151151 case Intrinsic::init_trampoline:
152152 return -1u;
153153 case Intrinsic::lifetime_end:
154 Len = I->getOperand(1);
154 Len = I->getOperand(0);
155155 break;
156156 }
157157 }
286286
287287 /// handleFreeWithNonTrivialDependency - Handle frees of entire structures whose
288288 /// dependency is a store to a field of that structure.
289 bool DSE::handleFreeWithNonTrivialDependency(Instruction *F, MemDepResult Dep) {
289 bool DSE::handleFreeWithNonTrivialDependency(/*FIXME: Call*/Instruction *F, MemDepResult Dep) {
290290 AliasAnalysis &AA = getAnalysis();
291291
292292 Instruction *Dependency = Dep.getInst();
296296 Value *DepPointer = getPointerOperand(Dependency)->getUnderlyingObject();
297297
298298 // Check for aliasing.
299 if (AA.alias(F->getOperand(1), 1, DepPointer, 1) !=
299 if (AA.alias(F->getOperand(0), 1, DepPointer, 1) !=
300300 AliasAnalysis::MustAlias)
301301 return false;
302302
270270 e.function = C->getCalledFunction();
271271 e.opcode = Expression::CALL;
272272
273 for (CallInst::op_iterator I = C->op_begin()+1, E = C->op_end();
273 for (CallInst::op_iterator I = C->op_begin(), E = C->op_end() - 1;
274274 I != E; ++I)
275275 e.varargs.push_back(lookup_or_add(*I));
276276
451451 return nextValueNumber++;
452452 }
453453
454 for (unsigned i = 1; i < C->getNumOperands(); ++i) {
454 for (unsigned i = 0, e = C->getNumOperands() - 1; i < e; ++i) {
455455 uint32_t c_vn = lookup_or_add(C->getOperand(i));
456456 uint32_t cd_vn = lookup_or_add(local_cdep->getOperand(i));
457457 if (c_vn != cd_vn) {
507507 valueNumbering[C] = nextValueNumber;
508508 return nextValueNumber++;
509509 }
510 for (unsigned i = 1; i < C->getNumOperands(); ++i) {
510 for (unsigned i = 0, e = C->getNumOperands() - 1; i < e; ++i) {
511511 uint32_t c_vn = lookup_or_add(C->getOperand(i));
512512 uint32_t cd_vn = lookup_or_add(cdep->getOperand(i));
513513 if (c_vn != cd_vn) {
743743 const Type *ArgTys[3] = { M->getRawDest()->getType(),
744744 M->getRawSource()->getType(),
745745 M->getLength()->getType() };
746 M->setOperand(0,Intrinsic::getDeclaration(Mod, Intrinsic::memcpy, ArgTys, 3));
746 M->setCalledFunction(Intrinsic::getDeclaration(Mod, Intrinsic::memcpy, ArgTys, 3));
747747
748748 // MemDep may have over conservative information about this instruction, just
749749 // conservatively flush it from the cache.
964964 isSafeGEP(GEPI, AI, GEPOffset, Info);
965965 if (!Info.isUnsafe)
966966 isSafeForScalarRepl(GEPI, AI, GEPOffset, Info);
967 } else if (MemIntrinsic *MI = dyn_cast(UI)) {
967 } else if (MemIntrinsic *MI = dyn_cast(User)) {
968968 ConstantInt *Length = dyn_cast(MI->getLength());
969969 if (Length)
970970 isSafeMemAccess(AI, Offset, Length->getZExtValue(), 0,
971 UI.getOperandNo() == 1, Info);
971 UI.getOperandNo() == 0, Info);
972972 else
973973 MarkUnsafe(Info);
974974 } else if (LoadInst *LI = dyn_cast(User)) {
13151315 }
13161316
13171317 // Process each element of the aggregate.
1318 Value *TheFn = MI->getOperand(0);
1318 Value *TheFn = MI->getCalledValue();
13191319 const Type *BytePtrTy = MI->getRawDest()->getType();
13201320 bool SROADest = MI->getRawDest() == Inst;
13211321
13721372 // If the stored element is zero (common case), just store a null
13731373 // constant.
13741374 Constant *StoreVal;
1375 if (ConstantInt *CI = dyn_cast(MI->getOperand(2))) {
1375 if (ConstantInt *CI = dyn_cast(MI->getOperand(1))) {
13761376 if (CI->isZero()) {
13771377 StoreVal = Constant::getNullValue(EltTy); // 0.0, null, 0, <0,0>
13781378 } else {
14351435 Value *Ops[] = {
14361436 SROADest ? EltPtr : OtherElt, // Dest ptr
14371437 SROADest ? OtherElt : EltPtr, // Src ptr
1438 ConstantInt::get(MI->getOperand(3)->getType(), EltSize), // Size
1438 ConstantInt::get(MI->getOperand(2)->getType(), EltSize), // Size
14391439 // Align
14401440 ConstantInt::get(Type::getInt32Ty(MI->getContext()), OtherEltAlign),
14411441 MI->getVolatileCst()
14501450 } else {
14511451 assert(isa(MI));
14521452 Value *Ops[] = {
1453 EltPtr, MI->getOperand(2), // Dest, Value,
1454 ConstantInt::get(MI->getOperand(3)->getType(), EltSize), // Size
1453 EltPtr, MI->getOperand(1), // Dest, Value,
1454 ConstantInt::get(MI->getOperand(2)->getType(), EltSize), // Size
14551455 Zero, // Align
14561456 ConstantInt::get(Type::getInt1Ty(MI->getContext()), 0) // isVolatile
14571457 };
17931793 if (isOffset) return false;
17941794
17951795 // If the memintrinsic isn't using the alloca as the dest, reject it.
1796 if (UI.getOperandNo() != 1) return false;
1796 if (UI.getOperandNo() != 0) return false;
17971797
17981798 // If the source of the memcpy/move is not a constant global, reject it.
17991799 if (!PointsToConstantGlobal(MI->getSource()))
109109 return 0;
110110
111111 // Extract some information from the instruction
112 Value *Dst = CI->getOperand(1);
113 Value *Src = CI->getOperand(2);
112 Value *Dst = CI->getOperand(0);
113 Value *Src = CI->getOperand(1);
114114
115115 // See if we can get the length of the input string.
116116 uint64_t Len = GetStringLength(Src);
161161 return 0;
162162
163163 // Extract some information from the instruction
164 Value *Dst = CI->getOperand(1);
165 Value *Src = CI->getOperand(2);
164 Value *Dst = CI->getOperand(0);
165 Value *Src = CI->getOperand(1);
166166 uint64_t Len;
167167
168168 // We don't do anything if length is not constant
169 if (ConstantInt *LengthArg = dyn_cast(CI->getOperand(3)))
169 if (ConstantInt *LengthArg = dyn_cast(CI->getOperand(2)))
170170 Len = LengthArg->getZExtValue();
171171 else
172172 return 0;
206206 FT->getParamType(0) != FT->getReturnType())
207207 return 0;
208208
209 Value *SrcStr = CI->getOperand(1);
209 Value *SrcStr = CI->getOperand(0);
210210
211211 // If the second operand is non-constant, see if we can compute the length
212212 // of the input string and turn this into memchr.
213 ConstantInt *CharC = dyn_cast(CI->getOperand(2));
213 ConstantInt *CharC = dyn_cast(CI->getOperand(1));
214214 if (CharC == 0) {
215215 // These optimizations require TargetData.
216216 if (!TD) return 0;
219219 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
220220 return 0;
221221
222 return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
222 return EmitMemChr(SrcStr, CI->getOperand(1), // include nul.
223223 ConstantInt::get(TD->getIntPtrType(*Context), Len),
224224 B, TD);
225225 }
264264 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
265265 return 0;
266266
267 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
267 Value *Str1P = CI->getOperand(0), *Str2P = CI->getOperand(1);
268268 if (Str1P == Str2P) // strcmp(x,x) -> 0
269269 return ConstantInt::get(CI->getType(), 0);
270270
313313 !FT->getParamType(2)->isIntegerTy())
314314 return 0;
315315
316 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
316 Value *Str1P = CI->getOperand(0), *Str2P = CI->getOperand(1);
317317 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
318318 return ConstantInt::get(CI->getType(), 0);
319319
320320 // Get the length argument if it is constant.
321321 uint64_t Length;
322 if (ConstantInt *LengthArg = dyn_cast(CI->getOperand(3)))
322 if (ConstantInt *LengthArg = dyn_cast(CI->getOperand(2)))
323323 Length = LengthArg->getZExtValue();
324324 else
325325 return 0;
364364 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
365365 return 0;
366366
367 Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
367 Value *Dst = CI->getOperand(0), *Src = CI->getOperand(1);
368368 if (Dst == Src) // strcpy(x,x) -> x
369369 return Src;
370370
380380 if (OptChkCall)
381381 EmitMemCpyChk(Dst, Src,
382382 ConstantInt::get(TD->getIntPtrType(*Context), Len),
383 CI->getOperand(3), B, TD);
383 CI->getOperand(2), B, TD);
384384 else
385385 EmitMemCpy(Dst, Src,
386386 ConstantInt::get(TD->getIntPtrType(*Context), Len),
401401 !FT->getParamType(2)->isIntegerTy())
402402 return 0;
403403
404 Value *Dst = CI->getOperand(1);
405 Value *Src = CI->getOperand(2);
406 Value *LenOp = CI->getOperand(3);
404 Value *Dst = CI->getOperand(0);
405 Value *Src = CI->getOperand(1);
406 Value *LenOp = CI->getOperand(2);
407407
408408 // See if we can get the length of the input string.
409409 uint64_t SrcLen = GetStringLength(Src);
451451 !FT->getReturnType()->isIntegerTy())
452452 return 0;
453453
454 Value *Src = CI->getOperand(1);
454 Value *Src = CI->getOperand(0);
455455
456456 // Constant folding: strlen("xyz") -> 3
457457 if (uint64_t Len = GetStringLength(Src))
476476 !FT->getParamType(1)->isPointerTy())
477477 return 0;
478478
479 Value *EndPtr = CI->getOperand(2);
479 Value *EndPtr = CI->getOperand(1);
480480 if (isa(EndPtr)) {
481481 CI->setOnlyReadsMemory();
482482 CI->addAttribute(1, Attribute::NoCapture);
499499 return 0;
500500
501501 // fold strstr(x, x) -> x.
502 if (CI->getOperand(1) == CI->getOperand(2))
503 return B.CreateBitCast(CI->getOperand(1), CI->getType());
502 if (CI->getOperand(0) == CI->getOperand(1))
503 return B.CreateBitCast(CI->getOperand(0), CI->getType());
504504
505505 // See if either input string is a constant string.
506506 std::string SearchStr, ToFindStr;
507 bool HasStr1 = GetConstantStringInfo(CI->getOperand(1), SearchStr);
508 bool HasStr2 = GetConstantStringInfo(CI->getOperand(2), ToFindStr);
507 bool HasStr1 = GetConstantStringInfo(CI->getOperand(0), SearchStr);
508 bool HasStr2 = GetConstantStringInfo(CI->getOperand(1), ToFindStr);
509509
510510 // fold strstr(x, "") -> x.
511511 if (HasStr2 && ToFindStr.empty())
512 return B.CreateBitCast(CI->getOperand(1), CI->getType());
512 return B.CreateBitCast(CI->getOperand(0), CI->getType());
513513
514514 // If both strings are known, constant fold it.
515515 if (HasStr1 && HasStr2) {
519519 return Constant::getNullValue(CI->getType());
520520
521521 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
522 Value *Result = CastToCStr(CI->getOperand(1), B);
522 Value *Result = CastToCStr(CI->getOperand(0), B);
523523 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
524524 return B.CreateBitCast(Result, CI->getType());
525525 }
526526
527527 // fold strstr(x, "y") -> strchr(x, 'y').
528528 if (HasStr2 && ToFindStr.size() == 1)
529 return B.CreateBitCast(EmitStrChr(CI->getOperand(1), ToFindStr[0], B, TD),
529 return B.CreateBitCast(EmitStrChr(CI->getOperand(0), ToFindStr[0], B, TD),
530530 CI->getType());
531531 return 0;
532532 }
544544 !FT->getReturnType()->isIntegerTy(32))
545545 return 0;
546546
547 Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
547 Value *LHS = CI->getOperand(0), *RHS = CI->getOperand(1);
548548
549549 if (LHS == RHS) // memcmp(s,s,x) -> 0
550550 return Constant::getNullValue(CI->getType());
551551
552552 // Make sure we have a constant length.
553 ConstantInt *LenC = dyn_cast(CI->getOperand(3));
553 ConstantInt *LenC = dyn_cast(CI->getOperand(2));
554554 if (!LenC) return 0;
555555 uint64_t Len = LenC->getZExtValue();
556556
594594 return 0;
595595
596596 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
597 EmitMemCpy(CI->getOperand(1), CI->getOperand(2),
598 CI->getOperand(3), 1, false, B, TD);
599 return CI->getOperand(1);
597 EmitMemCpy(CI->getOperand(0), CI->getOperand(1),
598 CI->getOperand(2), 1, false, B, TD);
599 return CI->getOperand(0);
600600 }
601601 };
602602
616616 return 0;
617617
618618 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
619 EmitMemMove(CI->getOperand(1), CI->getOperand(2),
620 CI->getOperand(3), 1, false, B, TD);
621 return CI->getOperand(1);
619 EmitMemMove(CI->getOperand(0), CI->getOperand(1),
620 CI->getOperand(2), 1, false, B, TD);
621 return CI->getOperand(0);
622622 }
623623 };
624624
638638 return 0;
639639
640640 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
641 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
642 false);
643 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), false, B, TD);
644 return CI->getOperand(1);
641 Value *Val = B.CreateIntCast(CI->getOperand(1), Type::getInt8Ty(*Context),
642 false);
643 EmitMemSet(CI->getOperand(0), Val, CI->getOperand(2), false, B, TD);
644 return CI->getOperand(0);
645645 }
646646 };
647647
662662 !FT->getParamType(0)->isFloatingPointTy())
663663 return 0;
664664
665 Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
665 Value *Op1 = CI->getOperand(0), *Op2 = CI->getOperand(1);
666666 if (ConstantFP *Op1C = dyn_cast(Op1)) {
667667 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
668668 return Op1C;
716716 !FT->getParamType(0)->isFloatingPointTy())
717717 return 0;
718718
719 Value *Op = CI->getOperand(1);
719 Value *Op = CI->getOperand(0);
720720 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
721721 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
722722 Value *LdExpArg = 0;
768768 return 0;
769769
770770 // If this is something like 'floor((double)floatval)', convert to floorf.
771 FPExtInst *Cast = dyn_cast(CI->getOperand(1));
771 FPExtInst *Cast = dyn_cast(CI->getOperand(0));
772772 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
773773 return 0;
774774
797797 !FT->getParamType(0)->isIntegerTy())
798798 return 0;
799799
800 Value *Op = CI->getOperand(1);
800 Value *Op = CI->getOperand(0);
801801
802802 // Constant fold.
803803 if (ConstantInt *CI = dyn_cast(Op)) {
833833 return 0;
834834
835835 // isdigit(c) -> (c-'0')
836 Value *Op = CI->getOperand(1);
836 Value *Op = CI->getOperand(0);
837837 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
838838 "isdigittmp");
839839 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
854854 return 0;
855855
856856 // isascii(c) -> c
857 Value *Op = CI->getOperand(1);
857 Value *Op = CI->getOperand(0);
858858 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
859859 "isascii");
860860 return B.CreateZExt(Op, CI->getType());
873873 return 0;
874874
875875 // abs(x) -> x >s -1 ? x : -x
876 Value *Op = CI->getOperand(1);
876 Value *Op = CI->getOperand(0);
877877 Value *Pos = B.CreateICmpSGT(Op,
878878 Constant::getAllOnesValue(Op->getType()),
879879 "ispos");
895895 return 0;
896896
897897 // isascii(c) -> c & 0x7f
898 return B.CreateAnd(CI->getOperand(1),
898 return B.CreateAnd(CI->getOperand(0),
899899 ConstantInt::get(CI->getType(),0x7F));
900900 }
901901 };
918918
919919 // Check for a fixed format string.
920920 std::string FormatStr;
921 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
921 if (!GetConstantStringInfo(CI->getOperand(0), FormatStr))
922922 return 0;
923923
924924 // Empty format string -> noop.
950950 }
951951
952952 // Optimize specific format strings.
953 // printf("%c", chr) --> putchar(*(i8*)dst)
953 // printf("%c", chr) --> putchar(chr)
954954 if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
955 CI->getOperand(2)->getType()->isIntegerTy()) {
956 Value *Res = EmitPutChar(CI->getOperand(2), B, TD);
955 CI->getOperand(1)->getType()->isIntegerTy()) {
956 Value *Res = EmitPutChar(CI->getOperand(1), B, TD);
957957
958958 if (CI->use_empty()) return CI;
959959 return B.CreateIntCast(Res, CI->getType(), true);
961961
962962 // printf("%s\n", str) --> puts(str)
963963 if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
964 CI->getOperand(2)->getType()->isPointerTy() &&
964 CI->getOperand(1)->getType()->isPointerTy() &&
965965 CI->use_empty()) {
966 EmitPutS(CI->getOperand(2), B, TD);
966 EmitPutS(CI->getOperand(1), B, TD);
967967 return CI;
968968 }
969969 return 0;
984984
985985 // Check for a fixed format string.
986986 std::string FormatStr;
987 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
987 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
988988 return 0;
989989
990990 // If we just have a format string (nothing else crazy) transform it.
999999 if (!TD) return 0;
10001000
10011001 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1002 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
1002 EmitMemCpy(CI->getOperand(0), CI->getOperand(1), // Copy the nul byte.
10031003 ConstantInt::get(TD->getIntPtrType(*Context),
10041004 FormatStr.size()+1), 1, false, B, TD);
10051005 return ConstantInt::get(CI->getType(), FormatStr.size());
10131013 // Decode the second character of the format string.
10141014 if (FormatStr[1] == 'c') {
10151015 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
1016 if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0;
1017 Value *V = B.CreateTrunc(CI->getOperand(3),
1016 if (!CI->getOperand(2)->getType()->isIntegerTy()) return 0;
1017 Value *V = B.CreateTrunc(CI->getOperand(2),
10181018 Type::getInt8Ty(*Context), "char");
1019 Value *Ptr = CastToCStr(CI->getOperand(1), B);
1019 Value *Ptr = CastToCStr(CI->getOperand(0), B);
10201020 B.CreateStore(V, Ptr);
10211021 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
10221022 "nul");
10301030 if (!TD) return 0;
10311031
10321032 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
1033 if (!CI->getOperand(3)->getType()->isPointerTy()) return 0;
1034
1035 Value *Len = EmitStrLen(CI->getOperand(3), B, TD);
1033 if (!CI->getOperand(2)->getType()->isPointerTy()) return 0;
1034
1035 Value *Len = EmitStrLen(CI->getOperand(2), B, TD);
10361036 Value *IncLen = B.CreateAdd(Len,
10371037 ConstantInt::get(Len->getType(), 1),
10381038 "leninc");
1039 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, false, B, TD);
1039 EmitMemCpy(CI->getOperand(0), CI->getOperand(2), IncLen, 1, false, B, TD);
10401040
10411041 // The sprintf result is the unincremented number of bytes in the string.
10421042 return B.CreateIntCast(Len, CI->getType(), false);
10601060 return 0;
10611061
10621062 // Get the element size and count.
1063 ConstantInt *SizeC = dyn_cast(CI->getOperand(2));
1064 ConstantInt *CountC = dyn_cast(CI->getOperand(3));
1063 ConstantInt *SizeC = dyn_cast(CI->getOperand(1));
1064 ConstantInt *CountC = dyn_cast(CI->getOperand(2));
10651065 if (!SizeC || !CountC) return 0;
10661066 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
10671067
10711071
10721072 // If this is writing one byte, turn it into fputc.
10731073 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1074 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
1075 EmitFPutC(Char, CI->getOperand(4), B, TD);
1074 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(0), B), "char");
1075 EmitFPutC(Char, CI->getOperand(3), B, TD);
10761076 return ConstantInt::get(CI->getType(), 1);
10771077 }
10781078
10961096 return 0;
10971097
10981098 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1099 uint64_t Len = GetStringLength(CI->getOperand(1));
1099 uint64_t Len = GetStringLength(CI->getOperand(0));
11001100 if (!Len) return 0;
1101 EmitFWrite(CI->getOperand(1),
1101 EmitFWrite(CI->getOperand(0),
11021102 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
1103 CI->getOperand(2), B, TD);
1103 CI->getOperand(1), B, TD);
11041104 return CI; // Known to have no uses (see above).
11051105 }
11061106 };
11191119
11201120 // All the optimizations depend on the format string.
11211121 std::string FormatStr;
1122 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1122 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
11231123 return 0;
11241124
11251125 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
11311131 // These optimizations require TargetData.
11321132 if (!TD) return 0;
11331133
1134 EmitFWrite(CI->getOperand(2),
1134 EmitFWrite(CI->getOperand(1),
11351135 ConstantInt::get(TD->getIntPtrType(*Context),
11361136 FormatStr.size()),
1137 CI->getOperand(1), B, TD);
1137 CI->getOperand(0), B, TD);
11381138 return ConstantInt::get(CI->getType(), FormatStr.size());
11391139 }
11401140
11451145
11461146 // Decode the second character of the format string.
11471147 if (FormatStr[1] == 'c') {
1148 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
1149 if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0;
1150 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B, TD);
1148 // fprintf(F, "%c", chr) --> fputc(chr, F)
1149 if (!CI->getOperand(2)->getType()->isIntegerTy()) return 0;
1150 EmitFPutC(CI->getOperand(2), CI->getOperand(0), B, TD);
11511151 return ConstantInt::get(CI->getType(), 1);
11521152 }
11531153
11541154 if (FormatStr[1] == 's') {
1155 // fprintf(F, "%s", str) -> fputs(str, F)
1156 if (!CI->getOperand(3)->getType()->isPointerTy() || !CI->use_empty())
1155 // fprintf(F, "%s", str) --> fputs(str, F)
1156 if (!CI->getOperand(2)->getType()->isPointerTy() || !CI->use_empty())
11571157 return 0;
1158 EmitFPutS(CI->getOperand(3), CI->getOperand(1), B, TD);
1158 EmitFPutS(CI->getOperand(2), CI->getOperand(0), B, TD);
11591159 return CI;
11601160 }
11611161 return 0;
249249 // If we are passing this argument into call as the corresponding
250250 // argument operand, then the argument is dynamically constant.
251251 // Otherwise, we cannot transform this function safely.
252 if (CI->getOperand(ArgNo+1) == Arg)
252 if (CI->getOperand(ArgNo) == Arg)
253253 return true;
254254 }
255255
441441 // required PHI nodes, add entries into the PHI node for the actual
442442 // parameters passed into the tail-recursive call.
443443 for (unsigned i = 0, e = CI->getNumOperands()-1; i != e; ++i)
444 ArgumentPHIs[i]->addIncoming(CI->getOperand(i+1), BB);
444 ArgumentPHIs[i]->addIncoming(CI->getOperand(i), BB);
445445
446446 // If we are introducing an accumulator variable to eliminate the recursion,
447447 // do so now. Note that we _know_ that no subsequent tail recursion
381381 std::vector
382382 Constraints = IA->ParseConstraints();
383383
384 unsigned ArgNo = 1; // ArgNo - The operand of the CallInst.
384 unsigned ArgNo = 0; // ArgNo - The operand of the CallInst.
385385 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
386386 TargetLowering::AsmOperandInfo OpInfo(Constraints[i]);
387387
449449
450450 if (CallInst *CI = dyn_cast(U)) {
451451 InlineAsm *IA = dyn_cast(CI->getCalledValue());
452 if (IA == 0) return true;
452 if (!IA) return true;
453453
454454 // If this is a memory operand, we're cool, otherwise bail out.
455455 if (!IsOperandAMemoryOperand(CI, IA, I, TLI))
394394 FT->getParamType(2) != TD->getIntPtrType(Context) ||
395395 FT->getParamType(3) != TD->getIntPtrType(Context))
396396 return false;
397
398 if (isFoldable(4, 3, false)) {
399 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3),
397
398 if (isFoldable(3, 2, false)) {
399 EmitMemCpy(CI->getOperand(0), CI->getOperand(1), CI->getOperand(2),
400400 1, false, B, TD);
401 replaceCall(CI->getOperand(1));
401 replaceCall(CI->getOperand(0));
402402 return true;
403403 }
404404 return false;
417417 FT->getParamType(2) != TD->getIntPtrType(Context) ||
418418 FT->getParamType(3) != TD->getIntPtrType(Context))
419419 return false;
420
421 if (isFoldable(4, 3, false)) {
422 EmitMemMove(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3),
420
421 if (isFoldable(3, 2, false)) {
422 EmitMemMove(CI->getOperand(0), CI->getOperand(1), CI->getOperand(2),
423423 1, false, B, TD);
424 replaceCall(CI->getOperand(1));
424 replaceCall(CI->getOperand(0));
425425 return true;
426426 }
427427 return false;
435435 FT->getParamType(2) != TD->getIntPtrType(Context) ||
436436 FT->getParamType(3) != TD->getIntPtrType(Context))
437437 return false;
438
439 if (isFoldable(4, 3, false)) {
440 Value *Val = B.CreateIntCast(CI->getOperand(2), B.getInt8Ty(),
438
439 if (isFoldable(3, 2, false)) {
440 Value *Val = B.CreateIntCast(CI->getOperand(1), B.getInt8Ty(),
441441 false);
442 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), false, B, TD);
443 replaceCall(CI->getOperand(1));
442 EmitMemSet(CI->getOperand(0), Val, CI->getOperand(2), false, B, TD);
443 replaceCall(CI->getOperand(0));
444444 return true;
445445 }
446446 return false;
461461 // st[rp]cpy_chk call which may fail at runtime if the size is too long.
462462 // TODO: It might be nice to get a maximum length out of the possible
463463 // string lengths for varying.
464 if (isFoldable(3, 2, true)) {
465 Value *Ret = EmitStrCpy(CI->getOperand(1), CI->getOperand(2), B, TD,
464 if (isFoldable(2, 1, true)) {
465 Value *Ret = EmitStrCpy(CI->getOperand(0), CI->getOperand(1), B, TD,
466466 Name.substr(2, 6));
467467 replaceCall(Ret);
468468 return true;
478478 !FT->getParamType(2)->isIntegerTy() ||
479479 FT->getParamType(3) != TD->getIntPtrType(Context))
480480 return false;
481
482 if (isFoldable(4, 3, false)) {
483 Value *Ret = EmitStrNCpy(CI->getOperand(1), CI->getOperand(2),
484 CI->getOperand(3), B, TD, Name.substr(2, 7));
481
482 if (isFoldable(3, 2, false)) {
483 Value *Ret = EmitStrNCpy(CI->getOperand(0), CI->getOperand(1),
484 CI->getOperand(2), B, TD, Name.substr(2, 7));
485485 replaceCall(Ret);
486486 return true;
487487 }
6565
6666 // Next, create the new invoke instruction, inserting it at the end
6767 // of the old basic block.
68 SmallVector InvokeArgs(CI->op_begin()+1, CI->op_end());
68 SmallVector InvokeArgs(CI->op_begin(), CI->op_end() - 1);
6969 InvokeInst *II =
7070 InvokeInst::Create(CI->getCalledValue(), Split, InvokeDest,
7171 InvokeArgs.begin(), InvokeArgs.end(),
18461846 default: Out << " cc" << CI->getCallingConv(); break;
18471847 }
18481848
1849 Operand = CI->getCalledValue();
18491850 const PointerType *PTy = cast(Operand->getType());
18501851 const FunctionType *FTy = cast(PTy->getElementType());
18511852 const Type *RetTy = FTy->getReturnType();
18691870 writeOperand(Operand, true);
18701871 }
18711872 Out << '(';
1872 for (unsigned op = 1, Eop = I.getNumOperands(); op < Eop; ++op) {
1873 if (op > 1)
1873 for (unsigned op = 0, Eop = CI->getNumOperands() - 1; op < Eop; ++op) {
1874 if (op > 0)
18741875 Out << ", ";
1875 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op));
1876 writeParamOperand(CI->getOperand(op), PAL.getParamAttributes(op + 1));
18761877 }
18771878 Out << ')';
18781879 if (PAL.getFnAttributes() != Attribute::None)
19161917 writeOperand(Operand, true);
19171918 }
19181919 Out << '(';
1919 for (unsigned op = 0, Eop = I.getNumOperands() - 3; op < Eop; ++op) {
1920 for (unsigned op = 0, Eop = II->getNumOperands() - 3; op < Eop; ++op) {
19201921 if (op)
19211922 Out << ", ";
1922 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op + 1));
1923 writeParamOperand(II->getOperand(op), PAL.getParamAttributes(op + 1));
19231924 }
19241925
19251926 Out << ')';
337337 if (isLoadH || isLoadL || isMovL || isMovSD || isShufPD ||
338338 isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
339339 std::vector Idxs;
340 Value *Op0 = CI->getOperand(1);
340 Value *Op0 = CI->getOperand(0);
341341 ShuffleVectorInst *SI = NULL;
342342 if (isLoadH || isLoadL) {
343343 Value *Op1 = UndefValue::get(Op0->getType());
344 Value *Addr = new BitCastInst(CI->getOperand(2),
344 Value *Addr = new BitCastInst(CI->getOperand(1),
345345 Type::getDoublePtrTy(C),
346346 "upgraded.", CI);
347347 Value *Load = new LoadInst(Addr, "upgraded.", false, 8, CI);
374374 SI = new ShuffleVectorInst(ZeroV, Op0, Mask, "upgraded.", CI);
375375 } else if (isMovSD ||
376376 isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
377 Value *Op1 = CI->getOperand(2);
377 Value *Op1 = CI->getOperand(1);
378378 if (isMovSD) {
379379 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
380380 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
388388 Value *Mask = ConstantVector::get(Idxs);
389389 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
390390 } else if (isShufPD) {
391 Value *Op1 = CI->getOperand(2);
392 unsigned MaskVal = cast(CI->getOperand(3))->getZExtValue();
391 Value *Op1 = CI->getOperand(1);
392 unsigned MaskVal = cast(CI->getOperand(2))->getZExtValue();
393393 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), MaskVal & 1));
394394 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C),
395395 ((MaskVal >> 1) & 1)+2));
409409 CI->eraseFromParent();
410410 } else if (F->getName() == "llvm.x86.sse41.pmulld") {
411411 // Upgrade this set of intrinsics into vector multiplies.
412 Instruction *Mul = BinaryOperator::CreateMul(CI->getOperand(1),
413 CI->getOperand(2),
412 Instruction *Mul = BinaryOperator::CreateMul(CI->getOperand(0),
413 CI->getOperand(1),
414414 CI->getName(),
415415 CI);
4