blob: 33dfcc032af1ac83d5838e7f4d73e6c2dea4ee89 [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Devang Pateladd58652009-09-23 18:32:25 +000015#include "LLVMContextImpl.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000016#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Evan Cheng1d9d4bd2009-09-10 04:36:43 +000020#include "llvm/Module.h"
Dan Gohmand2a251f2009-07-17 21:33:58 +000021#include "llvm/Operator.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000022#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000023#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000024#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000025#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000026using namespace llvm;
27
Chris Lattner3e13b8c2008-01-02 23:42:30 +000028//===----------------------------------------------------------------------===//
29// CallSite Class
30//===----------------------------------------------------------------------===//
31
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000032User::op_iterator CallSite::getCallee() const {
33 Instruction *II(getInstruction());
34 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000035 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000036 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000037}
38
Gordon Henriksen14a55692007-12-10 02:14:30 +000039//===----------------------------------------------------------------------===//
40// TerminatorInst Class
41//===----------------------------------------------------------------------===//
42
43// Out of line virtual method, so the vtable, etc has a home.
44TerminatorInst::~TerminatorInst() {
45}
46
Gabor Greiff6caff662008-05-10 08:32:32 +000047//===----------------------------------------------------------------------===//
48// UnaryInstruction Class
49//===----------------------------------------------------------------------===//
50
Gordon Henriksen14a55692007-12-10 02:14:30 +000051// Out of line virtual method, so the vtable, etc has a home.
52UnaryInstruction::~UnaryInstruction() {
53}
54
Chris Lattnerafdb3de2005-01-29 00:35:16 +000055//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000056// SelectInst Class
57//===----------------------------------------------------------------------===//
58
59/// areInvalidOperands - Return a string if the specified operands are invalid
60/// for a select operation, otherwise return null.
61const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
62 if (Op1->getType() != Op2->getType())
63 return "both values to select must have same type";
64
65 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
66 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000067 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000068 return "vector select condition element type must be i1";
69 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
70 if (ET == 0)
71 return "selected values for vector select must be vectors";
72 if (ET->getNumElements() != VT->getNumElements())
73 return "vector select requires selected vectors to have "
74 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000075 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000076 return "select condition must be i1 or <n x i1>";
77 }
78 return 0;
79}
80
81
82//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000083// PHINode Class
84//===----------------------------------------------------------------------===//
85
86PHINode::PHINode(const PHINode &PN)
87 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +000088 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +000089 ReservedSpace(PN.getNumOperands()) {
90 Use *OL = OperandList;
91 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +000092 OL[i] = PN.getOperand(i);
93 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +000094 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +000095 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000096}
97
Gordon Henriksen14a55692007-12-10 02:14:30 +000098PHINode::~PHINode() {
Jay Foadbbb91f22011-01-16 15:30:52 +000099 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000100}
101
102// removeIncomingValue - Remove an incoming value. This is useful if a
103// predecessor basic block is deleted.
104Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
105 unsigned NumOps = getNumOperands();
106 Use *OL = OperandList;
107 assert(Idx*2 < NumOps && "BB not in PHI node!");
108 Value *Removed = OL[Idx*2];
109
110 // Move everything after this operand down.
111 //
112 // FIXME: we could just swap with the end of the list, then erase. However,
113 // client might not expect this to happen. The code as it is thrashes the
114 // use/def lists, which is kinda lame.
115 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
116 OL[i-2] = OL[i];
117 OL[i-2+1] = OL[i+1];
118 }
119
120 // Nuke the last value.
121 OL[NumOps-2].set(0);
122 OL[NumOps-2+1].set(0);
123 NumOperands = NumOps-2;
124
125 // If the PHI node is dead, because it has zero entries, nuke it now.
126 if (NumOps == 2 && DeletePHIIfEmpty) {
127 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000128 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000129 eraseFromParent();
130 }
131 return Removed;
132}
133
Jay Foade98f29d2011-04-01 08:00:58 +0000134/// growOperands - grow operands - This grows the operand list in response
135/// to a push_back style of operation. This grows the number of ops by 1.5
136/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000137///
Jay Foade98f29d2011-04-01 08:00:58 +0000138void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000139 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +0000140 unsigned NumOps = e*3/2;
141 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000142
143 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000144 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000145 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000146 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000147 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +0000148 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000149}
150
Nate Begemanb3923212005-08-04 23:24:19 +0000151/// hasConstantValue - If the specified PHI node always merges together the same
152/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000153Value *PHINode::hasConstantValue() const {
154 // Exploit the fact that phi nodes always have at least one entry.
155 Value *ConstantValue = getIncomingValue(0);
156 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
157 if (getIncomingValue(i) != ConstantValue)
158 return 0; // Incoming values not all the same.
159 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000160}
161
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000162
163//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000164// CallInst Implementation
165//===----------------------------------------------------------------------===//
166
Gordon Henriksen14a55692007-12-10 02:14:30 +0000167CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000168}
169
Chris Lattner054ba2c2007-02-13 00:58:44 +0000170void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000171 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000172 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000173
Misha Brukmanb1c93172005-04-21 23:48:37 +0000174 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000175 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000176 (void)FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000177
Chris Lattner054ba2c2007-02-13 00:58:44 +0000178 assert((NumParams == FTy->getNumParams() ||
179 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000180 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000181 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000182 assert((i >= FTy->getNumParams() ||
183 FTy->getParamType(i) == Params[i]->getType()) &&
184 "Calling a function with a bad signature!");
Gabor Greif6d673952010-07-16 09:38:02 +0000185 OperandList[i] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000186 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000187}
188
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000189void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000190 assert(NumOperands == 3 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000191 Op<-1>() = Func;
192 Op<0>() = Actual1;
193 Op<1>() = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000194
195 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000196 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000197 (void)FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000198
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000199 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000200 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000201 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000202 assert((0 >= FTy->getNumParams() ||
203 FTy->getParamType(0) == Actual1->getType()) &&
204 "Calling a function with a bad signature!");
205 assert((1 >= FTy->getNumParams() ||
206 FTy->getParamType(1) == Actual2->getType()) &&
207 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000208}
209
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000210void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000211 assert(NumOperands == 2 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000212 Op<-1>() = Func;
213 Op<0>() = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000214
215 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000216 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000217 (void)FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000218
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000219 assert((FTy->getNumParams() == 1 ||
220 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000221 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000222 assert((0 == FTy->getNumParams() ||
223 FTy->getParamType(0) == Actual->getType()) &&
224 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000225}
226
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000227void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000228 assert(NumOperands == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000229 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000230
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000231 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000232 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000233 (void)FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000234
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000235 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000236}
237
Daniel Dunbar4975db62009-07-25 04:41:11 +0000238CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000239 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000240 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
241 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000242 Instruction::Call,
243 OperandTraits<CallInst>::op_end(this) - 2,
244 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000245 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000246 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000247}
248
Daniel Dunbar4975db62009-07-25 04:41:11 +0000249CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000250 BasicBlock *InsertAtEnd)
251 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
252 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000253 Instruction::Call,
254 OperandTraits<CallInst>::op_end(this) - 2,
255 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000256 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000257 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000258}
Daniel Dunbar4975db62009-07-25 04:41:11 +0000259CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000260 Instruction *InsertBefore)
261 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
262 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000263 Instruction::Call,
264 OperandTraits<CallInst>::op_end(this) - 1,
265 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000266 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000267 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000268}
269
Daniel Dunbar4975db62009-07-25 04:41:11 +0000270CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000271 BasicBlock *InsertAtEnd)
272 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
273 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000274 Instruction::Call,
275 OperandTraits<CallInst>::op_end(this) - 1,
276 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000277 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000278 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000279}
280
Misha Brukmanb1c93172005-04-21 23:48:37 +0000281CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000282 : Instruction(CI.getType(), Instruction::Call,
283 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000284 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000285 setAttributes(CI.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000286 setTailCall(CI.isTailCall());
287 setCallingConv(CI.getCallingConv());
288
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000289 Use *OL = OperandList;
290 Use *InOL = CI.OperandList;
291 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000292 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000293 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000294}
295
Devang Patel4c758ea2008-09-25 21:00:45 +0000296void CallInst::addAttribute(unsigned i, Attributes attr) {
297 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000298 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000299 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000300}
301
Devang Patel4c758ea2008-09-25 21:00:45 +0000302void CallInst::removeAttribute(unsigned i, Attributes attr) {
303 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000304 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000305 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000306}
307
Devang Patelba3fa6c2008-09-23 23:03:40 +0000308bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000309 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000310 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000311 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000312 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000313 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000314}
315
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000316/// IsConstantOne - Return true only if val is constant int 1
317static bool IsConstantOne(Value *val) {
318 assert(val && "IsConstantOne does not work with NULL val");
319 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
320}
321
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000322static Instruction *createMalloc(Instruction *InsertBefore,
323 BasicBlock *InsertAtEnd, const Type *IntPtrTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000324 const Type *AllocTy, Value *AllocSize,
325 Value *ArraySize, Function *MallocF,
326 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000327 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000328 "createMalloc needs either InsertBefore or InsertAtEnd");
329
330 // malloc(type) becomes:
331 // bitcast (i8* malloc(typeSize)) to type*
332 // malloc(type, arraySize) becomes:
333 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000334 if (!ArraySize)
335 ArraySize = ConstantInt::get(IntPtrTy, 1);
336 else if (ArraySize->getType() != IntPtrTy) {
337 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000338 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
339 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000340 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000341 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
342 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000343 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000344
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000345 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000346 if (IsConstantOne(AllocSize)) {
347 AllocSize = ArraySize; // Operand * 1 = Operand
348 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
349 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
350 false /*ZExt*/);
351 // Malloc arg is constant product of type size and array size
352 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
353 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000354 // Multiply type size by the array size...
355 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000356 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
357 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000358 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000359 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
360 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000361 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000362 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000363
Victor Hernandez788eaab2009-09-18 19:20:02 +0000364 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000365 // Create the call to Malloc.
366 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
367 Module* M = BB->getParent()->getParent();
Duncan Sands9ed7b162009-10-06 15:40:36 +0000368 const Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000369 Value *MallocFunc = MallocF;
370 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000371 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000372 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000373 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000374 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000375 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000376 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000377 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000378 Result = MCall;
379 if (Result->getType() != AllocPtrType)
380 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000381 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000382 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000383 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000384 Result = MCall;
385 if (Result->getType() != AllocPtrType) {
386 InsertAtEnd->getInstList().push_back(MCall);
387 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000388 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000389 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000390 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000391 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000392 if (Function *F = dyn_cast<Function>(MallocFunc)) {
393 MCall->setCallingConv(F->getCallingConv());
394 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
395 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000396 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000397
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000398 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000399}
400
401/// CreateMalloc - Generate the IR for a call to malloc:
402/// 1. Compute the malloc call's argument as the specified type's size,
403/// possibly multiplied by the array size if the array size is not
404/// constant 1.
405/// 2. Call malloc with that argument.
406/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000407Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
408 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000409 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000410 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000411 const Twine &Name) {
412 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000413 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000414}
415
416/// CreateMalloc - Generate the IR for a call to malloc:
417/// 1. Compute the malloc call's argument as the specified type's size,
418/// possibly multiplied by the array size if the array size is not
419/// constant 1.
420/// 2. Call malloc with that argument.
421/// 3. Bitcast the result of the malloc call to the specified type.
422/// Note: This function does not add the bitcast to the basic block, that is the
423/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000424Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
425 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000426 Value *AllocSize, Value *ArraySize,
427 Function *MallocF, const Twine &Name) {
428 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000429 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000430}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000431
Victor Hernandeze2971492009-10-24 04:23:03 +0000432static Instruction* createFree(Value* Source, Instruction *InsertBefore,
433 BasicBlock *InsertAtEnd) {
434 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
435 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000436 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000437 "Can not free something of nonpointer type!");
438
439 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
440 Module* M = BB->getParent()->getParent();
441
442 const Type *VoidTy = Type::getVoidTy(M->getContext());
443 const Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
444 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000445 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000446 CallInst* Result = NULL;
447 Value *PtrCast = Source;
448 if (InsertBefore) {
449 if (Source->getType() != IntPtrTy)
450 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
451 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
452 } else {
453 if (Source->getType() != IntPtrTy)
454 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
455 Result = CallInst::Create(FreeFunc, PtrCast, "");
456 }
457 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000458 if (Function *F = dyn_cast<Function>(FreeFunc))
459 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000460
461 return Result;
462}
463
464/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000465Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
466 return createFree(Source, InsertBefore, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000467}
468
469/// CreateFree - Generate the IR for a call to the builtin free function.
470/// Note: This function does not add the call to the basic block, that is the
471/// responsibility of the caller.
472Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
473 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
474 assert(FreeCall && "CreateFree did not create a CallInst");
475 return FreeCall;
476}
477
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000478//===----------------------------------------------------------------------===//
479// InvokeInst Implementation
480//===----------------------------------------------------------------------===//
481
482void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000483 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000484 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000485 Op<-3>() = Fn;
486 Op<-2>() = IfNormal;
487 Op<-1>() = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000488 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000489 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000490 (void)FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000491
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000492 assert(((NumArgs == FTy->getNumParams()) ||
493 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000494 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000495
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000496 Use *OL = OperandList;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000497 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000498 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000499 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000500 "Invoking a function with a bad signature!");
501
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000502 OL[i] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000503 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000504}
505
Misha Brukmanb1c93172005-04-21 23:48:37 +0000506InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000507 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000508 OperandTraits<InvokeInst>::op_end(this)
509 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000510 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000511 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000512 setCallingConv(II.getCallingConv());
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000513 Use *OL = OperandList, *InOL = II.OperandList;
514 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000515 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000516 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000517}
518
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000519BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
520 return getSuccessor(idx);
521}
522unsigned InvokeInst::getNumSuccessorsV() const {
523 return getNumSuccessors();
524}
525void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
526 return setSuccessor(idx, B);
527}
528
Devang Patelba3fa6c2008-09-23 23:03:40 +0000529bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000530 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000531 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000532 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000533 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000534 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000535}
536
Devang Patel4c758ea2008-09-25 21:00:45 +0000537void InvokeInst::addAttribute(unsigned i, Attributes attr) {
538 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000539 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000540 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000541}
542
Devang Patel4c758ea2008-09-25 21:00:45 +0000543void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
544 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000545 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000546 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000547}
548
Duncan Sands5208d1a2007-11-28 17:07:01 +0000549
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000550//===----------------------------------------------------------------------===//
551// ReturnInst Implementation
552//===----------------------------------------------------------------------===//
553
Chris Lattner2195fc42007-02-24 00:55:48 +0000554ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000555 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000556 OperandTraits<ReturnInst>::op_end(this) -
557 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000558 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000559 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000560 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000561 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000562}
563
Owen Anderson55f1c092009-08-13 21:58:54 +0000564ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
565 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000566 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
567 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000568 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000569 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000570}
Owen Anderson55f1c092009-08-13 21:58:54 +0000571ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
572 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000573 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
574 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000575 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000576 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000577}
Owen Anderson55f1c092009-08-13 21:58:54 +0000578ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
579 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000580 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000581}
582
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000583unsigned ReturnInst::getNumSuccessorsV() const {
584 return getNumSuccessors();
585}
586
Devang Patelae682fb2008-02-26 17:56:20 +0000587/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
588/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000589void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000590 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000591}
592
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000593BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000594 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000595 return 0;
596}
597
Devang Patel59643e52008-02-23 00:35:18 +0000598ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000599}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000600
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000601//===----------------------------------------------------------------------===//
602// UnwindInst Implementation
603//===----------------------------------------------------------------------===//
604
Owen Anderson55f1c092009-08-13 21:58:54 +0000605UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
606 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
607 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000608}
Owen Anderson55f1c092009-08-13 21:58:54 +0000609UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
610 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
611 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000612}
613
614
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000615unsigned UnwindInst::getNumSuccessorsV() const {
616 return getNumSuccessors();
617}
618
619void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000620 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000621}
622
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000623BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000624 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000625 return 0;
626}
627
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000628//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000629// UnreachableInst Implementation
630//===----------------------------------------------------------------------===//
631
Owen Anderson55f1c092009-08-13 21:58:54 +0000632UnreachableInst::UnreachableInst(LLVMContext &Context,
633 Instruction *InsertBefore)
634 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
635 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000636}
Owen Anderson55f1c092009-08-13 21:58:54 +0000637UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
638 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
639 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000640}
641
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000642unsigned UnreachableInst::getNumSuccessorsV() const {
643 return getNumSuccessors();
644}
645
646void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000647 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000648}
649
650BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000651 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000652 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000653}
654
655//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000656// BranchInst Implementation
657//===----------------------------------------------------------------------===//
658
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000659void BranchInst::AssertOK() {
660 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000661 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000662 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000663}
664
Chris Lattner2195fc42007-02-24 00:55:48 +0000665BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000666 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000667 OperandTraits<BranchInst>::op_end(this) - 1,
668 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000669 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000670 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000671}
672BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
673 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000674 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000675 OperandTraits<BranchInst>::op_end(this) - 3,
676 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000677 Op<-1>() = IfTrue;
678 Op<-2>() = IfFalse;
679 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000680#ifndef NDEBUG
681 AssertOK();
682#endif
683}
684
685BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000686 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000687 OperandTraits<BranchInst>::op_end(this) - 1,
688 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000689 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000690 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000691}
692
693BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
694 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000695 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000696 OperandTraits<BranchInst>::op_end(this) - 3,
697 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000698 Op<-1>() = IfTrue;
699 Op<-2>() = IfFalse;
700 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000701#ifndef NDEBUG
702 AssertOK();
703#endif
704}
705
706
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000707BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000708 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000709 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
710 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000711 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000712 if (BI.getNumOperands() != 1) {
713 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000714 Op<-3>() = BI.Op<-3>();
715 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000716 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000717 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000718}
719
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000720BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
721 return getSuccessor(idx);
722}
723unsigned BranchInst::getNumSuccessorsV() const {
724 return getNumSuccessors();
725}
726void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
727 setSuccessor(idx, B);
728}
729
730
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000731//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000732// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000733//===----------------------------------------------------------------------===//
734
Owen Andersonb6b25302009-07-14 23:09:55 +0000735static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000736 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000737 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000738 else {
739 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000740 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000741 assert(Amt->getType()->isIntegerTy() &&
742 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000743 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000744 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000745}
746
Victor Hernandez8acf2952009-10-23 21:09:37 +0000747AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
748 const Twine &Name, Instruction *InsertBefore)
749 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
750 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
751 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000752 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000753 setName(Name);
754}
755
756AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
757 const Twine &Name, BasicBlock *InsertAtEnd)
758 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
759 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
760 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000761 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000762 setName(Name);
763}
764
765AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
766 Instruction *InsertBefore)
767 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
768 getAISize(Ty->getContext(), 0), InsertBefore) {
769 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000770 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000771 setName(Name);
772}
773
774AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
775 BasicBlock *InsertAtEnd)
776 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
777 getAISize(Ty->getContext(), 0), InsertAtEnd) {
778 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000779 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000780 setName(Name);
781}
782
783AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
784 const Twine &Name, Instruction *InsertBefore)
785 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000786 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000787 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000788 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000789 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000790}
791
Victor Hernandez8acf2952009-10-23 21:09:37 +0000792AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
793 const Twine &Name, BasicBlock *InsertAtEnd)
794 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000795 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000796 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000797 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000798 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000799}
800
Gordon Henriksen14a55692007-12-10 02:14:30 +0000801// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000802AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000803}
804
Victor Hernandez8acf2952009-10-23 21:09:37 +0000805void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000806 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000807 assert(Align <= MaximumAlignment &&
808 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000809 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000810 assert(getAlignment() == Align && "Alignment representation error!");
811}
812
Victor Hernandez8acf2952009-10-23 21:09:37 +0000813bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000814 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +0000815 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000816 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000817}
818
Victor Hernandez8acf2952009-10-23 21:09:37 +0000819const Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000820 return getType()->getElementType();
821}
822
Chris Lattner8b291e62008-11-26 02:54:17 +0000823/// isStaticAlloca - Return true if this alloca is in the entry block of the
824/// function and is a constant size. If so, the code generator will fold it
825/// into the prolog/epilog code, so it is basically free.
826bool AllocaInst::isStaticAlloca() const {
827 // Must be constant size.
828 if (!isa<ConstantInt>(getArraySize())) return false;
829
830 // Must be in the entry block.
831 const BasicBlock *Parent = getParent();
832 return Parent == &Parent->getParent()->front();
833}
834
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000835//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000836// LoadInst Implementation
837//===----------------------------------------------------------------------===//
838
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000839void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000840 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000841 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000842}
843
Daniel Dunbar4975db62009-07-25 04:41:11 +0000844LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000845 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000846 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000847 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000848 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000849 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000850 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000851}
852
Daniel Dunbar4975db62009-07-25 04:41:11 +0000853LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000854 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000855 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000856 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000857 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000858 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000859 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000860}
861
Daniel Dunbar4975db62009-07-25 04:41:11 +0000862LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000863 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000864 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000865 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000866 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000867 setAlignment(0);
868 AssertOK();
869 setName(Name);
870}
871
Daniel Dunbar4975db62009-07-25 04:41:11 +0000872LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000873 unsigned Align, Instruction *InsertBef)
874 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
875 Load, Ptr, InsertBef) {
876 setVolatile(isVolatile);
877 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000878 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000879 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000880}
881
Daniel Dunbar4975db62009-07-25 04:41:11 +0000882LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000883 unsigned Align, BasicBlock *InsertAE)
884 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
885 Load, Ptr, InsertAE) {
886 setVolatile(isVolatile);
887 setAlignment(Align);
888 AssertOK();
889 setName(Name);
890}
891
Daniel Dunbar4975db62009-07-25 04:41:11 +0000892LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000893 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000894 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000895 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000896 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000897 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000898 AssertOK();
899 setName(Name);
900}
901
Daniel Dunbar27096822009-08-11 18:11:15 +0000902
903
904LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
905 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
906 Load, Ptr, InsertBef) {
907 setVolatile(false);
908 setAlignment(0);
909 AssertOK();
910 if (Name && Name[0]) setName(Name);
911}
912
913LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
914 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
915 Load, Ptr, InsertAE) {
916 setVolatile(false);
917 setAlignment(0);
918 AssertOK();
919 if (Name && Name[0]) setName(Name);
920}
921
922LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
923 Instruction *InsertBef)
924: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
925 Load, Ptr, InsertBef) {
926 setVolatile(isVolatile);
927 setAlignment(0);
928 AssertOK();
929 if (Name && Name[0]) setName(Name);
930}
931
932LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
933 BasicBlock *InsertAE)
934 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
935 Load, Ptr, InsertAE) {
936 setVolatile(isVolatile);
937 setAlignment(0);
938 AssertOK();
939 if (Name && Name[0]) setName(Name);
940}
941
Christopher Lamb84485702007-04-22 19:24:39 +0000942void LoadInst::setAlignment(unsigned Align) {
943 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000944 assert(Align <= MaximumAlignment &&
945 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000946 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
947 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +0000948 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +0000949}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000950
951//===----------------------------------------------------------------------===//
952// StoreInst Implementation
953//===----------------------------------------------------------------------===//
954
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000955void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +0000956 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +0000957 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000958 "Ptr must have pointer type!");
959 assert(getOperand(0)->getType() ==
960 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000961 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000962}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000963
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000964
965StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000966 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000967 OperandTraits<StoreInst>::op_begin(this),
968 OperandTraits<StoreInst>::operands(this),
969 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000970 Op<0>() = val;
971 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000972 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000973 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000974 AssertOK();
975}
976
977StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000978 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000979 OperandTraits<StoreInst>::op_begin(this),
980 OperandTraits<StoreInst>::operands(this),
981 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000982 Op<0>() = val;
983 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000984 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000985 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000986 AssertOK();
987}
988
Misha Brukmanb1c93172005-04-21 23:48:37 +0000989StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000990 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000991 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000992 OperandTraits<StoreInst>::op_begin(this),
993 OperandTraits<StoreInst>::operands(this),
994 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000995 Op<0>() = val;
996 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000997 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000998 setAlignment(0);
999 AssertOK();
1000}
1001
1002StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1003 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001004 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001005 OperandTraits<StoreInst>::op_begin(this),
1006 OperandTraits<StoreInst>::operands(this),
1007 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001008 Op<0>() = val;
1009 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001010 setVolatile(isVolatile);
1011 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001012 AssertOK();
1013}
1014
Misha Brukmanb1c93172005-04-21 23:48:37 +00001015StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001016 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001017 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001018 OperandTraits<StoreInst>::op_begin(this),
1019 OperandTraits<StoreInst>::operands(this),
1020 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001021 Op<0>() = val;
1022 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001023 setVolatile(isVolatile);
1024 setAlignment(Align);
1025 AssertOK();
1026}
1027
1028StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001029 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001030 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001031 OperandTraits<StoreInst>::op_begin(this),
1032 OperandTraits<StoreInst>::operands(this),
1033 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001034 Op<0>() = val;
1035 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001036 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001037 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001038 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001039}
1040
Christopher Lamb84485702007-04-22 19:24:39 +00001041void StoreInst::setAlignment(unsigned Align) {
1042 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001043 assert(Align <= MaximumAlignment &&
1044 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001045 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1046 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001047 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001048}
1049
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001050//===----------------------------------------------------------------------===//
1051// GetElementPtrInst Implementation
1052//===----------------------------------------------------------------------===//
1053
Christopher Lambedf07882007-12-17 01:12:55 +00001054static unsigned retrieveAddrSpace(const Value *Val) {
1055 return cast<PointerType>(Val->getType())->getAddressSpace();
1056}
1057
Gabor Greif21ba1842008-06-06 20:28:12 +00001058void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001059 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001060 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1061 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001062 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001063
Chris Lattner79807c3d2007-01-31 19:47:18 +00001064 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001065 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001066
1067 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001068}
1069
Daniel Dunbar4975db62009-07-25 04:41:11 +00001070void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001071 assert(NumOperands == 2 && "NumOperands not initialized?");
1072 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001073 OL[0] = Ptr;
1074 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001075
1076 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001077}
1078
Gabor Greiff6caff662008-05-10 08:32:32 +00001079GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001080 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001081 OperandTraits<GetElementPtrInst>::op_end(this)
1082 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001083 GEPI.getNumOperands()) {
1084 Use *OL = OperandList;
1085 Use *GEPIOL = GEPI.OperandList;
1086 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001087 OL[i] = GEPIOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001088 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001089}
1090
Chris Lattner82981202005-05-03 05:43:30 +00001091GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001092 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001093 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001094 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001095 GetElementPtr,
1096 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1097 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001098 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001099}
1100
1101GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001102 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001103 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001104 checkType(getIndexedType(Ptr->getType(),Idx)),
1105 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001106 GetElementPtr,
1107 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1108 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001109 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001110}
1111
Chris Lattner6090a422009-03-09 04:46:40 +00001112/// getIndexedType - Returns the type of the element that would be accessed with
1113/// a gep instruction with the specified parameters.
1114///
1115/// The Idxs pointer should point to a continuous piece of memory containing the
1116/// indices, either as Value* or uint64_t.
1117///
1118/// A null type is returned if the indices are invalid for the specified
1119/// pointer type.
1120///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001121template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001122static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1123 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001124 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1125 if (!PTy) return 0; // Type isn't a pointer type!
1126 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001127
Chris Lattner6090a422009-03-09 04:46:40 +00001128 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001129 if (NumIdx == 0)
1130 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001131
1132 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001133 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1134 // that contain opaque types) under the assumption that it will be resolved to
1135 // a sane type later.
1136 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001137 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001138
Dan Gohman1ecaf452008-05-31 00:58:22 +00001139 unsigned CurIdx = 1;
1140 for (; CurIdx != NumIdx; ++CurIdx) {
1141 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001142 if (!CT || CT->isPointerTy()) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001143 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001144 if (!CT->indexValid(Index)) return 0;
1145 Agg = CT->getTypeAtIndex(Index);
1146
1147 // If the new type forwards to another type, then it is in the middle
1148 // of being refined to another type (and hence, may have dropped all
1149 // references to what it was using before). So, use the new forwarded
1150 // type.
1151 if (const Type *Ty = Agg->getForwardedType())
1152 Agg = Ty;
1153 }
1154 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001155}
1156
Matthijs Kooijman04468622008-07-29 08:46:11 +00001157const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1158 Value* const *Idxs,
1159 unsigned NumIdx) {
1160 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1161}
1162
1163const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001164 Constant* const *Idxs,
1165 unsigned NumIdx) {
1166 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1167}
1168
1169const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Matthijs Kooijman04468622008-07-29 08:46:11 +00001170 uint64_t const *Idxs,
1171 unsigned NumIdx) {
1172 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1173}
1174
Chris Lattner82981202005-05-03 05:43:30 +00001175const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1176 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1177 if (!PTy) return 0; // Type isn't a pointer type!
1178
1179 // Check the pointer index.
1180 if (!PTy->indexValid(Idx)) return 0;
1181
Chris Lattnerc2233332005-05-03 16:44:45 +00001182 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001183}
1184
Chris Lattner45f15572007-04-14 00:12:57 +00001185
1186/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1187/// zeros. If so, the result pointer and the first operand have the same
1188/// value, just potentially different types.
1189bool GetElementPtrInst::hasAllZeroIndices() const {
1190 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1191 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1192 if (!CI->isZero()) return false;
1193 } else {
1194 return false;
1195 }
1196 }
1197 return true;
1198}
1199
Chris Lattner27058292007-04-27 20:35:56 +00001200/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1201/// constant integers. If so, the result pointer and the first operand have
1202/// a constant offset between them.
1203bool GetElementPtrInst::hasAllConstantIndices() const {
1204 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1205 if (!isa<ConstantInt>(getOperand(i)))
1206 return false;
1207 }
1208 return true;
1209}
1210
Dan Gohman1b849082009-09-07 23:54:19 +00001211void GetElementPtrInst::setIsInBounds(bool B) {
1212 cast<GEPOperator>(this)->setIsInBounds(B);
1213}
Chris Lattner45f15572007-04-14 00:12:57 +00001214
Nick Lewycky28a5f252009-09-27 21:33:04 +00001215bool GetElementPtrInst::isInBounds() const {
1216 return cast<GEPOperator>(this)->isInBounds();
1217}
1218
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001219//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001220// ExtractElementInst Implementation
1221//===----------------------------------------------------------------------===//
1222
1223ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001224 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001225 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001226 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001227 ExtractElement,
1228 OperandTraits<ExtractElementInst>::op_begin(this),
1229 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001230 assert(isValidOperands(Val, Index) &&
1231 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001232 Op<0>() = Val;
1233 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001234 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001235}
1236
1237ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001238 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001239 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001240 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001241 ExtractElement,
1242 OperandTraits<ExtractElementInst>::op_begin(this),
1243 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001244 assert(isValidOperands(Val, Index) &&
1245 "Invalid extractelement instruction operands!");
1246
Gabor Greif2d3024d2008-05-26 21:33:52 +00001247 Op<0>() = Val;
1248 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001249 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001250}
1251
Chris Lattner65511ff2006-10-05 06:24:58 +00001252
Chris Lattner54865b32006-04-08 04:05:48 +00001253bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001254 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001255 return false;
1256 return true;
1257}
1258
1259
Robert Bocchino23004482006-01-10 19:05:34 +00001260//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001261// InsertElementInst Implementation
1262//===----------------------------------------------------------------------===//
1263
Chris Lattner54865b32006-04-08 04:05:48 +00001264InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001265 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001266 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001267 : Instruction(Vec->getType(), InsertElement,
1268 OperandTraits<InsertElementInst>::op_begin(this),
1269 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001270 assert(isValidOperands(Vec, Elt, Index) &&
1271 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001272 Op<0>() = Vec;
1273 Op<1>() = Elt;
1274 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001275 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001276}
1277
Chris Lattner54865b32006-04-08 04:05:48 +00001278InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001279 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001280 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001281 : Instruction(Vec->getType(), InsertElement,
1282 OperandTraits<InsertElementInst>::op_begin(this),
1283 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001284 assert(isValidOperands(Vec, Elt, Index) &&
1285 "Invalid insertelement instruction operands!");
1286
Gabor Greif2d3024d2008-05-26 21:33:52 +00001287 Op<0>() = Vec;
1288 Op<1>() = Elt;
1289 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001290 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001291}
1292
Chris Lattner54865b32006-04-08 04:05:48 +00001293bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1294 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001295 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001296 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001297
Reid Spencerd84d35b2007-02-15 02:26:10 +00001298 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001299 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001300
Duncan Sands9dff9be2010-02-15 16:12:20 +00001301 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001302 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001303 return true;
1304}
1305
1306
Robert Bocchinoca27f032006-01-17 20:07:22 +00001307//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001308// ShuffleVectorInst Implementation
1309//===----------------------------------------------------------------------===//
1310
1311ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001312 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001313 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001314: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001315 cast<VectorType>(Mask->getType())->getNumElements()),
1316 ShuffleVector,
1317 OperandTraits<ShuffleVectorInst>::op_begin(this),
1318 OperandTraits<ShuffleVectorInst>::operands(this),
1319 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001320 assert(isValidOperands(V1, V2, Mask) &&
1321 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001322 Op<0>() = V1;
1323 Op<1>() = V2;
1324 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001325 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001326}
1327
1328ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001329 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001330 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001331: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1332 cast<VectorType>(Mask->getType())->getNumElements()),
1333 ShuffleVector,
1334 OperandTraits<ShuffleVectorInst>::op_begin(this),
1335 OperandTraits<ShuffleVectorInst>::operands(this),
1336 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001337 assert(isValidOperands(V1, V2, Mask) &&
1338 "Invalid shuffle vector instruction operands!");
1339
Gabor Greif2d3024d2008-05-26 21:33:52 +00001340 Op<0>() = V1;
1341 Op<1>() = V2;
1342 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001343 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001344}
1345
Mon P Wang25f01062008-11-10 04:46:22 +00001346bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001347 const Value *Mask) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001348 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001349 return false;
1350
1351 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001352 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001353 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001354
1355 // Check to see if Mask is valid.
1356 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
1357 const VectorType *VTy = cast<VectorType>(V1->getType());
1358 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
1359 if (ConstantInt* CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1360 if (CI->uge(VTy->getNumElements()*2))
1361 return false;
1362 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1363 return false;
1364 }
1365 }
1366 }
1367 else if (!isa<UndefValue>(Mask) && !isa<ConstantAggregateZero>(Mask))
1368 return false;
1369
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001370 return true;
1371}
1372
Chris Lattnerf724e342008-03-02 05:28:33 +00001373/// getMaskValue - Return the index from the shuffle mask for the specified
1374/// output result. This is either -1 if the element is undef or a number less
1375/// than 2*numelements.
1376int ShuffleVectorInst::getMaskValue(unsigned i) const {
1377 const Constant *Mask = cast<Constant>(getOperand(2));
1378 if (isa<UndefValue>(Mask)) return -1;
1379 if (isa<ConstantAggregateZero>(Mask)) return 0;
1380 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1381 assert(i < MaskCV->getNumOperands() && "Index out of range");
1382
1383 if (isa<UndefValue>(MaskCV->getOperand(i)))
1384 return -1;
1385 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1386}
1387
Dan Gohman12fce772008-05-15 19:50:34 +00001388//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001389// InsertValueInst Class
1390//===----------------------------------------------------------------------===//
1391
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001392void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001393 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001394 assert(NumOperands == 2 && "NumOperands not initialized?");
Frits van Bommel16ebe772010-12-05 20:50:26 +00001395 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idx, Idx + NumIdx) ==
1396 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001397 Op<0>() = Agg;
1398 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001399
Dan Gohmandd41bba2010-06-21 19:47:52 +00001400 Indices.append(Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001401 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001402}
1403
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001404void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001405 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001406 assert(NumOperands == 2 && "NumOperands not initialized?");
Frits van Bommel16ebe772010-12-05 20:50:26 +00001407 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idx) == Val->getType()
1408 && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001409 Op<0>() = Agg;
1410 Op<1>() = Val;
1411
1412 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001413 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001414}
1415
1416InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001417 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001418 OperandTraits<InsertValueInst>::op_begin(this), 2),
1419 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001420 Op<0>() = IVI.getOperand(0);
1421 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001422 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001423}
1424
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001425InsertValueInst::InsertValueInst(Value *Agg,
1426 Value *Val,
1427 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001428 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001429 Instruction *InsertBefore)
1430 : Instruction(Agg->getType(), InsertValue,
1431 OperandTraits<InsertValueInst>::op_begin(this),
1432 2, InsertBefore) {
1433 init(Agg, Val, Idx, Name);
1434}
1435
1436InsertValueInst::InsertValueInst(Value *Agg,
1437 Value *Val,
1438 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001439 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001440 BasicBlock *InsertAtEnd)
1441 : Instruction(Agg->getType(), InsertValue,
1442 OperandTraits<InsertValueInst>::op_begin(this),
1443 2, InsertAtEnd) {
1444 init(Agg, Val, Idx, Name);
1445}
1446
Dan Gohman0752bff2008-05-23 00:36:11 +00001447//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001448// ExtractValueInst Class
1449//===----------------------------------------------------------------------===//
1450
Gabor Greifcbcc4952008-06-06 21:06:32 +00001451void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001452 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001453 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001454
Dan Gohmandd41bba2010-06-21 19:47:52 +00001455 Indices.append(Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001456 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001457}
1458
Daniel Dunbar4975db62009-07-25 04:41:11 +00001459void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001460 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001461
1462 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001463 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001464}
1465
1466ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001467 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001468 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001469 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001470}
1471
Dan Gohman12fce772008-05-15 19:50:34 +00001472// getIndexedType - Returns the type of the element that would be extracted
1473// with an extractvalue instruction with the specified parameters.
1474//
1475// A null type is returned if the indices are invalid for the specified
1476// pointer type.
1477//
1478const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001479 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001480 unsigned NumIdx) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001481 for (unsigned CurIdx = 0; CurIdx != NumIdx; ++CurIdx) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001482 unsigned Index = Idxs[CurIdx];
Frits van Bommel16ebe772010-12-05 20:50:26 +00001483 // We can't use CompositeType::indexValid(Index) here.
1484 // indexValid() always returns true for arrays because getelementptr allows
1485 // out-of-bounds indices. Since we don't allow those for extractvalue and
1486 // insertvalue we need to check array indexing manually.
1487 // Since the only other types we can index into are struct types it's just
1488 // as easy to check those manually as well.
1489 if (const ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
1490 if (Index >= AT->getNumElements())
1491 return 0;
1492 } else if (const StructType *ST = dyn_cast<StructType>(Agg)) {
1493 if (Index >= ST->getNumElements())
1494 return 0;
1495 } else {
1496 // Not a valid type to index into.
1497 return 0;
1498 }
1499
1500 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001501
1502 // If the new type forwards to another type, then it is in the middle
1503 // of being refined to another type (and hence, may have dropped all
1504 // references to what it was using before). So, use the new forwarded
1505 // type.
1506 if (const Type *Ty = Agg->getForwardedType())
1507 Agg = Ty;
1508 }
Frits van Bommel16ebe772010-12-05 20:50:26 +00001509 return Agg;
Dan Gohman12fce772008-05-15 19:50:34 +00001510}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001511
Dan Gohman4a3125b2008-06-17 21:07:55 +00001512const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001513 unsigned Idx) {
1514 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001515}
1516
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001517//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001518// BinaryOperator Class
1519//===----------------------------------------------------------------------===//
1520
Chris Lattner2195fc42007-02-24 00:55:48 +00001521BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001522 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001523 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001524 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001525 OperandTraits<BinaryOperator>::op_begin(this),
1526 OperandTraits<BinaryOperator>::operands(this),
1527 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001528 Op<0>() = S1;
1529 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001530 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001531 setName(Name);
1532}
1533
1534BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001535 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001536 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001537 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001538 OperandTraits<BinaryOperator>::op_begin(this),
1539 OperandTraits<BinaryOperator>::operands(this),
1540 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001541 Op<0>() = S1;
1542 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001543 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001544 setName(Name);
1545}
1546
1547
1548void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001549 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001550 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001551 assert(LHS->getType() == RHS->getType() &&
1552 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001553#ifndef NDEBUG
1554 switch (iType) {
1555 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001556 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001557 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001558 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001559 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001560 "Tried to create an integer operation on a non-integer type!");
1561 break;
1562 case FAdd: case FSub:
1563 case FMul:
1564 assert(getType() == LHS->getType() &&
1565 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001566 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001567 "Tried to create a floating-point operation on a "
1568 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001569 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001570 case UDiv:
1571 case SDiv:
1572 assert(getType() == LHS->getType() &&
1573 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001574 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001575 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001576 "Incorrect operand type (not integer) for S/UDIV");
1577 break;
1578 case FDiv:
1579 assert(getType() == LHS->getType() &&
1580 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001581 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001582 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001583 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001584 case URem:
1585 case SRem:
1586 assert(getType() == LHS->getType() &&
1587 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001588 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001589 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001590 "Incorrect operand type (not integer) for S/UREM");
1591 break;
1592 case FRem:
1593 assert(getType() == LHS->getType() &&
1594 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001595 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001596 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001597 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001598 case Shl:
1599 case LShr:
1600 case AShr:
1601 assert(getType() == LHS->getType() &&
1602 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001603 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001604 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001605 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001606 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001607 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001608 case And: case Or:
1609 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001610 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001611 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001612 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001613 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001614 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001615 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001616 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001617 default:
1618 break;
1619 }
1620#endif
1621}
1622
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001623BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001624 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001625 Instruction *InsertBefore) {
1626 assert(S1->getType() == S2->getType() &&
1627 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001628 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001629}
1630
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001631BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001632 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001633 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001634 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001635 InsertAtEnd->getInstList().push_back(Res);
1636 return Res;
1637}
1638
Dan Gohman5476cfd2009-08-12 16:23:25 +00001639BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001640 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001641 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001642 return new BinaryOperator(Instruction::Sub,
1643 zero, Op,
1644 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001645}
1646
Dan Gohman5476cfd2009-08-12 16:23:25 +00001647BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001648 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001649 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001650 return new BinaryOperator(Instruction::Sub,
1651 zero, Op,
1652 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001653}
1654
Dan Gohman4ab44202009-12-18 02:58:50 +00001655BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1656 Instruction *InsertBefore) {
1657 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1658 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1659}
1660
1661BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1662 BasicBlock *InsertAtEnd) {
1663 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1664 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1665}
1666
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001667BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1668 Instruction *InsertBefore) {
1669 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1670 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1671}
1672
1673BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1674 BasicBlock *InsertAtEnd) {
1675 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1676 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1677}
1678
Dan Gohman5476cfd2009-08-12 16:23:25 +00001679BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001680 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001681 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001682 return new BinaryOperator(Instruction::FSub,
1683 zero, Op,
1684 Op->getType(), Name, InsertBefore);
1685}
1686
Dan Gohman5476cfd2009-08-12 16:23:25 +00001687BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001688 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001689 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001690 return new BinaryOperator(Instruction::FSub,
1691 zero, Op,
1692 Op->getType(), Name, InsertAtEnd);
1693}
1694
Dan Gohman5476cfd2009-08-12 16:23:25 +00001695BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001696 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001697 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001698 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001699 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001700 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001701 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001702 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001703 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001704 }
1705
1706 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001707 Op->getType(), Name, InsertBefore);
1708}
1709
Dan Gohman5476cfd2009-08-12 16:23:25 +00001710BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001711 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001712 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001713 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001714 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001715 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001716 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001717 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001718 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001719 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001720 }
1721
1722 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001723 Op->getType(), Name, InsertAtEnd);
1724}
1725
1726
1727// isConstantAllOnes - Helper function for several functions below
1728static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001729 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1730 return CI->isAllOnesValue();
1731 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1732 return CV->isAllOnesValue();
1733 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001734}
1735
Owen Andersonbb2501b2009-07-13 22:18:28 +00001736bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001737 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1738 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001739 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1740 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001741 return false;
1742}
1743
Owen Andersonbb2501b2009-07-13 22:18:28 +00001744bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001745 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1746 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001747 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001748 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001749 return false;
1750}
1751
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001752bool BinaryOperator::isNot(const Value *V) {
1753 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1754 return (Bop->getOpcode() == Instruction::Xor &&
1755 (isConstantAllOnes(Bop->getOperand(1)) ||
1756 isConstantAllOnes(Bop->getOperand(0))));
1757 return false;
1758}
1759
Chris Lattner2c7d1772005-04-24 07:28:37 +00001760Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001761 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001762}
1763
Chris Lattner2c7d1772005-04-24 07:28:37 +00001764const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1765 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001766}
1767
Dan Gohmana5b96452009-06-04 22:49:04 +00001768Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001769 return cast<BinaryOperator>(BinOp)->getOperand(1);
1770}
1771
1772const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1773 return getFNegArgument(const_cast<Value*>(BinOp));
1774}
1775
Chris Lattner2c7d1772005-04-24 07:28:37 +00001776Value *BinaryOperator::getNotArgument(Value *BinOp) {
1777 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1778 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1779 Value *Op0 = BO->getOperand(0);
1780 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001781 if (isConstantAllOnes(Op0)) return Op1;
1782
1783 assert(isConstantAllOnes(Op1));
1784 return Op0;
1785}
1786
Chris Lattner2c7d1772005-04-24 07:28:37 +00001787const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1788 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001789}
1790
1791
1792// swapOperands - Exchange the two operands to this instruction. This
1793// instruction is safe to use on any binary instruction and does not
1794// modify the semantics of the instruction. If the instruction is
1795// order dependent (SetLT f.e.) the opcode is changed.
1796//
1797bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001798 if (!isCommutative())
1799 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001800 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001801 return false;
1802}
1803
Dan Gohman1b849082009-09-07 23:54:19 +00001804void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1805 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1806}
1807
1808void BinaryOperator::setHasNoSignedWrap(bool b) {
1809 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1810}
1811
1812void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00001813 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00001814}
1815
Nick Lewycky28a5f252009-09-27 21:33:04 +00001816bool BinaryOperator::hasNoUnsignedWrap() const {
1817 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1818}
1819
1820bool BinaryOperator::hasNoSignedWrap() const {
1821 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1822}
1823
1824bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00001825 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00001826}
1827
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001828//===----------------------------------------------------------------------===//
1829// CastInst Class
1830//===----------------------------------------------------------------------===//
1831
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001832// Just determine if this cast only deals with integral->integral conversion.
1833bool CastInst::isIntegerCast() const {
1834 switch (getOpcode()) {
1835 default: return false;
1836 case Instruction::ZExt:
1837 case Instruction::SExt:
1838 case Instruction::Trunc:
1839 return true;
1840 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00001841 return getOperand(0)->getType()->isIntegerTy() &&
1842 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001843 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001844}
1845
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001846bool CastInst::isLosslessCast() const {
1847 // Only BitCast can be lossless, exit fast if we're not BitCast
1848 if (getOpcode() != Instruction::BitCast)
1849 return false;
1850
1851 // Identity cast is always lossless
1852 const Type* SrcTy = getOperand(0)->getType();
1853 const Type* DstTy = getType();
1854 if (SrcTy == DstTy)
1855 return true;
1856
Reid Spencer8d9336d2006-12-31 05:26:44 +00001857 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00001858 if (SrcTy->isPointerTy())
1859 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001860 return false; // Other types have no identity values
1861}
1862
1863/// This function determines if the CastInst does not require any bits to be
1864/// changed in order to effect the cast. Essentially, it identifies cases where
1865/// no code gen is necessary for the cast, hence the name no-op cast. For
1866/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001867/// # bitcast i32* %x to i8*
1868/// # bitcast <2 x i32> %x to <4 x i16>
1869/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001870/// @brief Determine if the described cast is a no-op.
1871bool CastInst::isNoopCast(Instruction::CastOps Opcode,
1872 const Type *SrcTy,
1873 const Type *DestTy,
1874 const Type *IntPtrTy) {
1875 switch (Opcode) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001876 default:
1877 assert(!"Invalid CastOp");
1878 case Instruction::Trunc:
1879 case Instruction::ZExt:
1880 case Instruction::SExt:
1881 case Instruction::FPTrunc:
1882 case Instruction::FPExt:
1883 case Instruction::UIToFP:
1884 case Instruction::SIToFP:
1885 case Instruction::FPToUI:
1886 case Instruction::FPToSI:
1887 return false; // These always modify bits
1888 case Instruction::BitCast:
1889 return true; // BitCast never modifies bits.
1890 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001891 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001892 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001893 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001894 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001895 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001896 }
1897}
1898
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001899/// @brief Determine if a cast is a no-op.
1900bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1901 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
1902}
1903
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001904/// This function determines if a pair of casts can be eliminated and what
1905/// opcode should be used in the elimination. This assumes that there are two
1906/// instructions like this:
1907/// * %F = firstOpcode SrcTy %x to MidTy
1908/// * %S = secondOpcode MidTy %F to DstTy
1909/// The function returns a resultOpcode so these two casts can be replaced with:
1910/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1911/// If no such cast is permited, the function returns 0.
1912unsigned CastInst::isEliminableCastPair(
1913 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1914 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1915{
1916 // Define the 144 possibilities for these two cast instructions. The values
1917 // in this matrix determine what to do in a given situation and select the
1918 // case in the switch below. The rows correspond to firstOp, the columns
1919 // correspond to secondOp. In looking at the table below, keep in mind
1920 // the following cast properties:
1921 //
1922 // Size Compare Source Destination
1923 // Operator Src ? Size Type Sign Type Sign
1924 // -------- ------------ ------------------- ---------------------
1925 // TRUNC > Integer Any Integral Any
1926 // ZEXT < Integral Unsigned Integer Any
1927 // SEXT < Integral Signed Integer Any
1928 // FPTOUI n/a FloatPt n/a Integral Unsigned
1929 // FPTOSI n/a FloatPt n/a Integral Signed
1930 // UITOFP n/a Integral Unsigned FloatPt n/a
1931 // SITOFP n/a Integral Signed FloatPt n/a
1932 // FPTRUNC > FloatPt n/a FloatPt n/a
1933 // FPEXT < FloatPt n/a FloatPt n/a
1934 // PTRTOINT n/a Pointer n/a Integral Unsigned
1935 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00001936 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001937 //
1938 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001939 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1940 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001941 // of the produced value (we no longer know the top-part is all zeros).
1942 // Further this conversion is often much more expensive for typical hardware,
1943 // and causes issues when building libgcc. We disallow fptosi+sext for the
1944 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001945 const unsigned numCastOps =
1946 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1947 static const uint8_t CastResults[numCastOps][numCastOps] = {
1948 // T F F U S F F P I B -+
1949 // R Z S P P I I T P 2 N T |
1950 // U E E 2 2 2 2 R E I T C +- secondOp
1951 // N X X U S F F N X N 2 V |
1952 // C T T I I P P C T T P T -+
1953 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1954 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1955 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001956 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1957 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001958 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1959 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1960 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1961 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1962 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1963 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1964 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1965 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00001966
1967 // If either of the casts are a bitcast from scalar to vector, disallow the
1968 // merging.
1969 if ((firstOp == Instruction::BitCast &&
1970 isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
1971 (secondOp == Instruction::BitCast &&
1972 isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
1973 return 0; // Disallowed
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001974
1975 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1976 [secondOp-Instruction::CastOpsBegin];
1977 switch (ElimCase) {
1978 case 0:
1979 // categorically disallowed
1980 return 0;
1981 case 1:
1982 // allowed, use first cast's opcode
1983 return firstOp;
1984 case 2:
1985 // allowed, use second cast's opcode
1986 return secondOp;
1987 case 3:
1988 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00001989 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00001990 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001991 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001992 return firstOp;
1993 return 0;
1994 case 4:
1995 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00001996 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00001997 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001998 return firstOp;
1999 return 0;
2000 case 5:
2001 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002002 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002003 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002004 return secondOp;
2005 return 0;
2006 case 6:
2007 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002008 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002009 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002010 return secondOp;
2011 return 0;
2012 case 7: {
2013 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002014 if (!IntPtrTy)
2015 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002016 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2017 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002018 if (MidSize >= PtrSize)
2019 return Instruction::BitCast;
2020 return 0;
2021 }
2022 case 8: {
2023 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2024 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2025 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002026 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2027 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002028 if (SrcSize == DstSize)
2029 return Instruction::BitCast;
2030 else if (SrcSize < DstSize)
2031 return firstOp;
2032 return secondOp;
2033 }
2034 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2035 return Instruction::ZExt;
2036 case 10:
2037 // fpext followed by ftrunc is allowed if the bit size returned to is
2038 // the same as the original, in which case its just a bitcast
2039 if (SrcTy == DstTy)
2040 return Instruction::BitCast;
2041 return 0; // If the types are not the same we can't eliminate it.
2042 case 11:
2043 // bitcast followed by ptrtoint is allowed as long as the bitcast
2044 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002045 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002046 return secondOp;
2047 return 0;
2048 case 12:
2049 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002050 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002051 return firstOp;
2052 return 0;
2053 case 13: {
2054 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002055 if (!IntPtrTy)
2056 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002057 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2058 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2059 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002060 if (SrcSize <= PtrSize && SrcSize == DstSize)
2061 return Instruction::BitCast;
2062 return 0;
2063 }
2064 case 99:
2065 // cast combination can't happen (error in input). This is for all cases
2066 // where the MidTy is not the same for the two cast instructions.
2067 assert(!"Invalid Cast Combination");
2068 return 0;
2069 default:
2070 assert(!"Error in CastResults table!!!");
2071 return 0;
2072 }
2073 return 0;
2074}
2075
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002076CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002077 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002078 // Construct and return the appropriate CastInst subclass
2079 switch (op) {
2080 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2081 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2082 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2083 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2084 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2085 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2086 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2087 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2088 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2089 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2090 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2091 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2092 default:
2093 assert(!"Invalid opcode provided");
2094 }
2095 return 0;
2096}
2097
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002098CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002099 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002100 // Construct and return the appropriate CastInst subclass
2101 switch (op) {
2102 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2103 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2104 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2105 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2106 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2107 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2108 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2109 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2110 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2111 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2112 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2113 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2114 default:
2115 assert(!"Invalid opcode provided");
2116 }
2117 return 0;
2118}
2119
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002120CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002121 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002122 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002123 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002124 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2125 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002126}
2127
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002128CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002129 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002130 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002131 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002132 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2133 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002134}
2135
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002136CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002137 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002138 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002139 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002140 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2141 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002142}
2143
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002144CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002145 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002146 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002147 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002148 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2149 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002150}
2151
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002152CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002153 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002154 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002155 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002156 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2157 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002158}
2159
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002160CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002161 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002162 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002163 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002164 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2165 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002166}
2167
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002168CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002169 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002170 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002171 assert(S->getType()->isPointerTy() && "Invalid cast");
2172 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002173 "Invalid cast");
2174
Duncan Sands9dff9be2010-02-15 16:12:20 +00002175 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002176 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2177 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002178}
2179
2180/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002181CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002182 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002183 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002184 assert(S->getType()->isPointerTy() && "Invalid cast");
2185 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002186 "Invalid cast");
2187
Duncan Sands9dff9be2010-02-15 16:12:20 +00002188 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002189 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2190 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002191}
2192
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002193CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002194 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002195 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002196 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002197 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002198 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2199 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002200 Instruction::CastOps opcode =
2201 (SrcBits == DstBits ? Instruction::BitCast :
2202 (SrcBits > DstBits ? Instruction::Trunc :
2203 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002204 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002205}
2206
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002207CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002208 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002209 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002210 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002211 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002212 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2213 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002214 Instruction::CastOps opcode =
2215 (SrcBits == DstBits ? Instruction::BitCast :
2216 (SrcBits > DstBits ? Instruction::Trunc :
2217 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002218 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002219}
2220
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002221CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002222 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002223 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002224 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002225 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002226 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2227 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002228 Instruction::CastOps opcode =
2229 (SrcBits == DstBits ? Instruction::BitCast :
2230 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002231 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002232}
2233
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002234CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002235 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002236 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002237 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002238 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002239 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2240 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002241 Instruction::CastOps opcode =
2242 (SrcBits == DstBits ? Instruction::BitCast :
2243 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002244 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002245}
2246
Duncan Sands55e50902008-01-06 10:12:28 +00002247// Check whether it is valid to call getCastOpcode for these types.
2248// This routine must be kept in sync with getCastOpcode.
2249bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2250 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2251 return false;
2252
2253 if (SrcTy == DestTy)
2254 return true;
2255
2256 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002257 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2258 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002259
2260 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002261 if (DestTy->isIntegerTy()) { // Casting to integral
2262 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002263 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002264 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002265 return true;
2266 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002267 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002268 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002269 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002270 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002271 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002272 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2273 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002274 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002275 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002276 return true;
2277 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002278 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002279 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002280 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002281 return false;
2282 }
2283 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002284 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002285 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002286 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002287 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002288 } else if (DestPTy->getBitWidth() == SrcBits) {
2289 return true; // float/int -> vector
2290 } else if (SrcTy->isX86_MMXTy()) {
2291 return DestPTy->getBitWidth() == 64; // MMX to 64-bit vector
2292 } else {
2293 return false;
Duncan Sands55e50902008-01-06 10:12:28 +00002294 }
Duncan Sands19d0b472010-02-16 11:11:14 +00002295 } else if (DestTy->isPointerTy()) { // Casting to pointer
2296 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002297 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002298 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002299 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002300 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002301 return false;
2302 }
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002303 } else if (DestTy->isX86_MMXTy()) {
2304 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
2305 return SrcPTy->getBitWidth() == 64; // 64-bit vector to MMX
2306 } else {
2307 return false;
2308 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002309 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002310 return false;
2311 }
2312}
2313
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002314// Provide a way to get a "cast" where the cast opcode is inferred from the
2315// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002316// logic in the castIsValid function below. This axiom should hold:
2317// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2318// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002319// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002320// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002321Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002322CastInst::getCastOpcode(
2323 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002324 // Get the bit sizes, we'll need these
2325 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002326 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2327 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002328
Duncan Sands55e50902008-01-06 10:12:28 +00002329 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2330 "Only first class types are castable!");
2331
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002332 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002333 if (DestTy->isIntegerTy()) { // Casting to integral
2334 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002335 if (DestBits < SrcBits)
2336 return Trunc; // int -> smaller int
2337 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002338 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002339 return SExt; // signed -> SEXT
2340 else
2341 return ZExt; // unsigned -> ZEXT
2342 } else {
2343 return BitCast; // Same size, No-op cast
2344 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002345 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002346 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002347 return FPToSI; // FP -> sint
2348 else
2349 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002350 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002351 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002352 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002353 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002354 return BitCast; // Same size, no-op cast
2355 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002356 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002357 "Casting from a value that is not first-class type");
2358 return PtrToInt; // ptr -> int
2359 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002360 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2361 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002362 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002363 return SIToFP; // sint -> FP
2364 else
2365 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002366 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002367 if (DestBits < SrcBits) {
2368 return FPTrunc; // FP -> smaller FP
2369 } else if (DestBits > SrcBits) {
2370 return FPExt; // FP -> larger FP
2371 } else {
2372 return BitCast; // same size, no-op cast
2373 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002374 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002375 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002376 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002377 PTy = NULL;
2378 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002379 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002380 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002381 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002382 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2383 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002384 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002385 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002386 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002387 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002388 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002389 return BitCast; // float/int -> vector
Dale Johannesendd224d22010-09-30 23:57:10 +00002390 } else if (SrcTy->isX86_MMXTy()) {
2391 assert(DestPTy->getBitWidth()==64 &&
2392 "Casting X86_MMX to vector of wrong width");
2393 return BitCast; // MMX to 64-bit vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002394 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002395 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002396 }
Duncan Sands19d0b472010-02-16 11:11:14 +00002397 } else if (DestTy->isPointerTy()) {
2398 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002399 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002400 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002401 return IntToPtr; // int -> ptr
2402 } else {
2403 assert(!"Casting pointer to other than pointer or int");
2404 }
Dale Johannesendd224d22010-09-30 23:57:10 +00002405 } else if (DestTy->isX86_MMXTy()) {
Bill Wendling3c704172010-10-03 00:46:06 +00002406 if (isa<VectorType>(SrcTy)) {
2407 assert(cast<VectorType>(SrcTy)->getBitWidth() == 64 &&
Dale Johannesendd224d22010-09-30 23:57:10 +00002408 "Casting vector of wrong width to X86_MMX");
2409 return BitCast; // 64-bit vector to MMX
2410 } else {
2411 assert(!"Illegal cast to X86_MMX");
2412 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002413 } else {
2414 assert(!"Casting to type that is not first-class");
2415 }
2416
2417 // If we fall through to here we probably hit an assertion cast above
2418 // and assertions are not turned on. Anything we return is an error, so
2419 // BitCast is as good a choice as any.
2420 return BitCast;
2421}
2422
2423//===----------------------------------------------------------------------===//
2424// CastInst SubClass Constructors
2425//===----------------------------------------------------------------------===//
2426
2427/// Check that the construction parameters for a CastInst are correct. This
2428/// could be broken out into the separate constructors but it is useful to have
2429/// it in one place and to eliminate the redundant code for getting the sizes
2430/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002431bool
2432CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002433
2434 // Check for type sanity on the arguments
2435 const Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002436 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2437 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002438 return false;
2439
2440 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002441 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2442 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002443
2444 // Switch on the opcode provided
2445 switch (op) {
2446 default: return false; // This is an input error
2447 case Instruction::Trunc:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002448 return SrcTy->isIntOrIntVectorTy() &&
2449 DstTy->isIntOrIntVectorTy()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002450 case Instruction::ZExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002451 return SrcTy->isIntOrIntVectorTy() &&
2452 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002453 case Instruction::SExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002454 return SrcTy->isIntOrIntVectorTy() &&
2455 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002456 case Instruction::FPTrunc:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002457 return SrcTy->isFPOrFPVectorTy() &&
2458 DstTy->isFPOrFPVectorTy() &&
Dan Gohman550c9af2008-08-14 20:04:46 +00002459 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002460 case Instruction::FPExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002461 return SrcTy->isFPOrFPVectorTy() &&
2462 DstTy->isFPOrFPVectorTy() &&
Dan Gohman550c9af2008-08-14 20:04:46 +00002463 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002464 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002465 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002466 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2467 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002468 return SVTy->getElementType()->isIntOrIntVectorTy() &&
2469 DVTy->getElementType()->isFPOrFPVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002470 SVTy->getNumElements() == DVTy->getNumElements();
2471 }
2472 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002473 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002474 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002475 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002476 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2477 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002478 return SVTy->getElementType()->isFPOrFPVectorTy() &&
2479 DVTy->getElementType()->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002480 SVTy->getNumElements() == DVTy->getNumElements();
2481 }
2482 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002483 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002484 case Instruction::PtrToInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002485 return SrcTy->isPointerTy() && DstTy->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002486 case Instruction::IntToPtr:
Duncan Sands19d0b472010-02-16 11:11:14 +00002487 return SrcTy->isIntegerTy() && DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002488 case Instruction::BitCast:
2489 // BitCast implies a no-op cast of type only. No bits change.
2490 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002491 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002492 return false;
2493
Duncan Sands55e50902008-01-06 10:12:28 +00002494 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002495 // these cases, the cast is okay if the source and destination bit widths
2496 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002497 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002498 }
2499}
2500
2501TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002502 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002503) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002504 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002505}
2506
2507TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002508 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002509) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002510 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002511}
2512
2513ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002514 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002515) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002516 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002517}
2518
2519ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002520 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002521) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002522 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002523}
2524SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002525 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002526) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002527 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002528}
2529
Jeff Cohencc08c832006-12-02 02:22:01 +00002530SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002531 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002532) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002533 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002534}
2535
2536FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002537 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002538) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002539 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002540}
2541
2542FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002543 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002544) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002545 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002546}
2547
2548FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002549 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002550) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002551 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002552}
2553
2554FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002555 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002556) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002557 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002558}
2559
2560UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002561 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002562) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002563 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002564}
2565
2566UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002567 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002568) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002569 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002570}
2571
2572SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002573 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002574) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002575 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002576}
2577
2578SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002579 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002580) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002581 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002582}
2583
2584FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002585 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002586) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002587 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002588}
2589
2590FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002591 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002592) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002593 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002594}
2595
2596FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002597 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002598) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002599 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002600}
2601
2602FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002603 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002604) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002605 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002606}
2607
2608PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002609 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002610) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002611 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002612}
2613
2614PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002615 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002616) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002617 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002618}
2619
2620IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002621 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002622) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002623 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002624}
2625
2626IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002627 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002628) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002629 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002630}
2631
2632BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002633 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002634) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002635 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002636}
2637
2638BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002639 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002640) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002641 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002642}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002643
2644//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002645// CmpInst Classes
2646//===----------------------------------------------------------------------===//
2647
Chris Lattneraec33da2010-01-22 06:25:37 +00002648void CmpInst::Anchor() const {}
2649
Nate Begemand2195702008-05-12 19:01:56 +00002650CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002651 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002652 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002653 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002654 OperandTraits<CmpInst>::op_begin(this),
2655 OperandTraits<CmpInst>::operands(this),
2656 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002657 Op<0>() = LHS;
2658 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002659 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002660 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002661}
Gabor Greiff6caff662008-05-10 08:32:32 +00002662
Nate Begemand2195702008-05-12 19:01:56 +00002663CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002664 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002665 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002666 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002667 OperandTraits<CmpInst>::op_begin(this),
2668 OperandTraits<CmpInst>::operands(this),
2669 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002670 Op<0>() = LHS;
2671 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002672 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002673 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002674}
2675
2676CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002677CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002678 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002679 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002680 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002681 if (InsertBefore)
2682 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2683 S1, S2, Name);
2684 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002685 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002686 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002687 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002688
2689 if (InsertBefore)
2690 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2691 S1, S2, Name);
2692 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002693 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002694 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002695}
2696
2697CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002698CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002699 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002700 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002701 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2702 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002703 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002704 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2705 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002706}
2707
2708void CmpInst::swapOperands() {
2709 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2710 IC->swapOperands();
2711 else
2712 cast<FCmpInst>(this)->swapOperands();
2713}
2714
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002715bool CmpInst::isCommutative() const {
2716 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002717 return IC->isCommutative();
2718 return cast<FCmpInst>(this)->isCommutative();
2719}
2720
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002721bool CmpInst::isEquality() const {
2722 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002723 return IC->isEquality();
2724 return cast<FCmpInst>(this)->isEquality();
2725}
2726
2727
Dan Gohman4e724382008-05-31 02:47:54 +00002728CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002729 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002730 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002731 case ICMP_EQ: return ICMP_NE;
2732 case ICMP_NE: return ICMP_EQ;
2733 case ICMP_UGT: return ICMP_ULE;
2734 case ICMP_ULT: return ICMP_UGE;
2735 case ICMP_UGE: return ICMP_ULT;
2736 case ICMP_ULE: return ICMP_UGT;
2737 case ICMP_SGT: return ICMP_SLE;
2738 case ICMP_SLT: return ICMP_SGE;
2739 case ICMP_SGE: return ICMP_SLT;
2740 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002741
Dan Gohman4e724382008-05-31 02:47:54 +00002742 case FCMP_OEQ: return FCMP_UNE;
2743 case FCMP_ONE: return FCMP_UEQ;
2744 case FCMP_OGT: return FCMP_ULE;
2745 case FCMP_OLT: return FCMP_UGE;
2746 case FCMP_OGE: return FCMP_ULT;
2747 case FCMP_OLE: return FCMP_UGT;
2748 case FCMP_UEQ: return FCMP_ONE;
2749 case FCMP_UNE: return FCMP_OEQ;
2750 case FCMP_UGT: return FCMP_OLE;
2751 case FCMP_ULT: return FCMP_OGE;
2752 case FCMP_UGE: return FCMP_OLT;
2753 case FCMP_ULE: return FCMP_OGT;
2754 case FCMP_ORD: return FCMP_UNO;
2755 case FCMP_UNO: return FCMP_ORD;
2756 case FCMP_TRUE: return FCMP_FALSE;
2757 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002758 }
2759}
2760
Reid Spencer266e42b2006-12-23 06:05:41 +00002761ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2762 switch (pred) {
2763 default: assert(! "Unknown icmp predicate!");
2764 case ICMP_EQ: case ICMP_NE:
2765 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2766 return pred;
2767 case ICMP_UGT: return ICMP_SGT;
2768 case ICMP_ULT: return ICMP_SLT;
2769 case ICMP_UGE: return ICMP_SGE;
2770 case ICMP_ULE: return ICMP_SLE;
2771 }
2772}
2773
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002774ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2775 switch (pred) {
2776 default: assert(! "Unknown icmp predicate!");
2777 case ICMP_EQ: case ICMP_NE:
2778 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2779 return pred;
2780 case ICMP_SGT: return ICMP_UGT;
2781 case ICMP_SLT: return ICMP_ULT;
2782 case ICMP_SGE: return ICMP_UGE;
2783 case ICMP_SLE: return ICMP_ULE;
2784 }
2785}
2786
Reid Spencer0286bc12007-02-28 22:00:54 +00002787/// Initialize a set of values that all satisfy the condition with C.
2788///
2789ConstantRange
2790ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2791 APInt Lower(C);
2792 APInt Upper(C);
2793 uint32_t BitWidth = C.getBitWidth();
2794 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002795 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002796 case ICmpInst::ICMP_EQ: Upper++; break;
2797 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00002798 case ICmpInst::ICMP_ULT:
2799 Lower = APInt::getMinValue(BitWidth);
2800 // Check for an empty-set condition.
2801 if (Lower == Upper)
2802 return ConstantRange(BitWidth, /*isFullSet=*/false);
2803 break;
2804 case ICmpInst::ICMP_SLT:
2805 Lower = APInt::getSignedMinValue(BitWidth);
2806 // Check for an empty-set condition.
2807 if (Lower == Upper)
2808 return ConstantRange(BitWidth, /*isFullSet=*/false);
2809 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00002810 case ICmpInst::ICMP_UGT:
2811 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002812 // Check for an empty-set condition.
2813 if (Lower == Upper)
2814 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002815 break;
2816 case ICmpInst::ICMP_SGT:
2817 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002818 // Check for an empty-set condition.
2819 if (Lower == Upper)
2820 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002821 break;
2822 case ICmpInst::ICMP_ULE:
2823 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002824 // Check for a full-set condition.
2825 if (Lower == Upper)
2826 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002827 break;
2828 case ICmpInst::ICMP_SLE:
2829 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002830 // Check for a full-set condition.
2831 if (Lower == Upper)
2832 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002833 break;
2834 case ICmpInst::ICMP_UGE:
2835 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002836 // Check for a full-set condition.
2837 if (Lower == Upper)
2838 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002839 break;
2840 case ICmpInst::ICMP_SGE:
2841 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002842 // Check for a full-set condition.
2843 if (Lower == Upper)
2844 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002845 break;
2846 }
2847 return ConstantRange(Lower, Upper);
2848}
2849
Dan Gohman4e724382008-05-31 02:47:54 +00002850CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002851 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002852 default: assert(!"Unknown cmp predicate!");
2853 case ICMP_EQ: case ICMP_NE:
2854 return pred;
2855 case ICMP_SGT: return ICMP_SLT;
2856 case ICMP_SLT: return ICMP_SGT;
2857 case ICMP_SGE: return ICMP_SLE;
2858 case ICMP_SLE: return ICMP_SGE;
2859 case ICMP_UGT: return ICMP_ULT;
2860 case ICMP_ULT: return ICMP_UGT;
2861 case ICMP_UGE: return ICMP_ULE;
2862 case ICMP_ULE: return ICMP_UGE;
2863
Reid Spencerd9436b62006-11-20 01:22:35 +00002864 case FCMP_FALSE: case FCMP_TRUE:
2865 case FCMP_OEQ: case FCMP_ONE:
2866 case FCMP_UEQ: case FCMP_UNE:
2867 case FCMP_ORD: case FCMP_UNO:
2868 return pred;
2869 case FCMP_OGT: return FCMP_OLT;
2870 case FCMP_OLT: return FCMP_OGT;
2871 case FCMP_OGE: return FCMP_OLE;
2872 case FCMP_OLE: return FCMP_OGE;
2873 case FCMP_UGT: return FCMP_ULT;
2874 case FCMP_ULT: return FCMP_UGT;
2875 case FCMP_UGE: return FCMP_ULE;
2876 case FCMP_ULE: return FCMP_UGE;
2877 }
2878}
2879
Reid Spencer266e42b2006-12-23 06:05:41 +00002880bool CmpInst::isUnsigned(unsigned short predicate) {
2881 switch (predicate) {
2882 default: return false;
2883 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2884 case ICmpInst::ICMP_UGE: return true;
2885 }
2886}
2887
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002888bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002889 switch (predicate) {
2890 default: return false;
2891 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2892 case ICmpInst::ICMP_SGE: return true;
2893 }
2894}
2895
2896bool CmpInst::isOrdered(unsigned short predicate) {
2897 switch (predicate) {
2898 default: return false;
2899 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2900 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2901 case FCmpInst::FCMP_ORD: return true;
2902 }
2903}
2904
2905bool CmpInst::isUnordered(unsigned short predicate) {
2906 switch (predicate) {
2907 default: return false;
2908 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2909 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2910 case FCmpInst::FCMP_UNO: return true;
2911 }
2912}
2913
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002914bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2915 switch(predicate) {
2916 default: return false;
2917 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2918 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2919 }
2920}
2921
2922bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2923 switch(predicate) {
2924 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2925 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2926 default: return false;
2927 }
2928}
2929
2930
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002931//===----------------------------------------------------------------------===//
2932// SwitchInst Implementation
2933//===----------------------------------------------------------------------===//
2934
Chris Lattnerbaf00152010-11-17 05:41:46 +00002935void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
2936 assert(Value && Default && NumReserved);
2937 ReservedSpace = NumReserved;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002938 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002939 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002940
Gabor Greif2d3024d2008-05-26 21:33:52 +00002941 OperandList[0] = Value;
2942 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002943}
2944
Chris Lattner2195fc42007-02-24 00:55:48 +00002945/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2946/// switch on and a default destination. The number of additional cases can
2947/// be specified here to make memory allocation more efficient. This
2948/// constructor can also autoinsert before another instruction.
2949SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2950 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002951 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2952 0, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00002953 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00002954}
2955
2956/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2957/// switch on and a default destination. The number of additional cases can
2958/// be specified here to make memory allocation more efficient. This
2959/// constructor also autoinserts at the end of the specified BasicBlock.
2960SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2961 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002962 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2963 0, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00002964 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00002965}
2966
Misha Brukmanb1c93172005-04-21 23:48:37 +00002967SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerbaf00152010-11-17 05:41:46 +00002968 : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) {
2969 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
2970 NumOperands = SI.getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002971 Use *OL = OperandList, *InOL = SI.OperandList;
Chris Lattnerbaf00152010-11-17 05:41:46 +00002972 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002973 OL[i] = InOL[i];
2974 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002975 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002976 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002977}
2978
Gordon Henriksen14a55692007-12-10 02:14:30 +00002979SwitchInst::~SwitchInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00002980 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002981}
2982
2983
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002984/// addCase - Add an entry to the switch instruction...
2985///
Chris Lattner47ac1872005-02-24 05:32:09 +00002986void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002987 unsigned OpNo = NumOperands;
2988 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00002989 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002990 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002991 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002992 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002993 OperandList[OpNo] = OnVal;
2994 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002995}
2996
2997/// removeCase - This method removes the specified successor from the switch
2998/// instruction. Note that this cannot be used to remove the default
2999/// destination (successor #0).
3000///
3001void SwitchInst::removeCase(unsigned idx) {
3002 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003003 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3004
3005 unsigned NumOps = getNumOperands();
3006 Use *OL = OperandList;
3007
Jay Foad14277722011-02-01 09:22:34 +00003008 // Overwrite this case with the end of the list.
3009 if ((idx + 1) * 2 != NumOps) {
3010 OL[idx * 2] = OL[NumOps - 2];
3011 OL[idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003012 }
3013
3014 // Nuke the last value.
3015 OL[NumOps-2].set(0);
3016 OL[NumOps-2+1].set(0);
3017 NumOperands = NumOps-2;
3018}
3019
Jay Foade98f29d2011-04-01 08:00:58 +00003020/// growOperands - grow operands - This grows the operand list in response
3021/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003022///
Jay Foade98f29d2011-04-01 08:00:58 +00003023void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003024 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003025 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003026
3027 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003028 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003029 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003030 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003031 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003032 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003033 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003034 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003035}
3036
3037
3038BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3039 return getSuccessor(idx);
3040}
3041unsigned SwitchInst::getNumSuccessorsV() const {
3042 return getNumSuccessors();
3043}
3044void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3045 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003046}
Chris Lattnerf22be932004-10-15 23:52:53 +00003047
Chris Lattner3ed871f2009-10-27 19:13:16 +00003048//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003049// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003050//===----------------------------------------------------------------------===//
3051
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003052void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003053 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003054 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003055 ReservedSpace = 1+NumDests;
3056 NumOperands = 1;
3057 OperandList = allocHungoffUses(ReservedSpace);
3058
3059 OperandList[0] = Address;
3060}
3061
3062
Jay Foade98f29d2011-04-01 08:00:58 +00003063/// growOperands - grow operands - This grows the operand list in response
3064/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003065///
Jay Foade98f29d2011-04-01 08:00:58 +00003066void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003067 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003068 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003069
3070 ReservedSpace = NumOps;
3071 Use *NewOps = allocHungoffUses(NumOps);
3072 Use *OldOps = OperandList;
3073 for (unsigned i = 0; i != e; ++i)
3074 NewOps[i] = OldOps[i];
3075 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003076 Use::zap(OldOps, OldOps + e, true);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003077}
3078
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003079IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3080 Instruction *InsertBefore)
3081: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003082 0, 0, InsertBefore) {
3083 init(Address, NumCases);
3084}
3085
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003086IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3087 BasicBlock *InsertAtEnd)
3088: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003089 0, 0, InsertAtEnd) {
3090 init(Address, NumCases);
3091}
3092
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003093IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3094 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003095 allocHungoffUses(IBI.getNumOperands()),
3096 IBI.getNumOperands()) {
3097 Use *OL = OperandList, *InOL = IBI.OperandList;
3098 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3099 OL[i] = InOL[i];
3100 SubclassOptionalData = IBI.SubclassOptionalData;
3101}
3102
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003103IndirectBrInst::~IndirectBrInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003104 dropHungoffUses();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003105}
3106
3107/// addDestination - Add a destination.
3108///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003109void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003110 unsigned OpNo = NumOperands;
3111 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003112 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003113 // Initialize some new operands.
3114 assert(OpNo < ReservedSpace && "Growing didn't work!");
3115 NumOperands = OpNo+1;
3116 OperandList[OpNo] = DestBB;
3117}
3118
3119/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003120/// indirectbr instruction.
3121void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003122 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3123
3124 unsigned NumOps = getNumOperands();
3125 Use *OL = OperandList;
3126
3127 // Replace this value with the last one.
3128 OL[idx+1] = OL[NumOps-1];
3129
3130 // Nuke the last value.
3131 OL[NumOps-1].set(0);
3132 NumOperands = NumOps-1;
3133}
3134
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003135BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003136 return getSuccessor(idx);
3137}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003138unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003139 return getNumSuccessors();
3140}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003141void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003142 setSuccessor(idx, B);
3143}
3144
3145//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003146// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003147//===----------------------------------------------------------------------===//
3148
Chris Lattnerf22be932004-10-15 23:52:53 +00003149// Define these methods here so vtables don't get emitted into every translation
3150// unit that uses these classes.
3151
Devang Patel11cf3f42009-10-27 22:16:29 +00003152GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3153 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003154}
3155
Devang Patel11cf3f42009-10-27 22:16:29 +00003156BinaryOperator *BinaryOperator::clone_impl() const {
3157 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003158}
3159
Devang Patel11cf3f42009-10-27 22:16:29 +00003160FCmpInst* FCmpInst::clone_impl() const {
3161 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003162}
3163
Devang Patel11cf3f42009-10-27 22:16:29 +00003164ICmpInst* ICmpInst::clone_impl() const {
3165 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003166}
3167
Devang Patel11cf3f42009-10-27 22:16:29 +00003168ExtractValueInst *ExtractValueInst::clone_impl() const {
3169 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003170}
3171
Devang Patel11cf3f42009-10-27 22:16:29 +00003172InsertValueInst *InsertValueInst::clone_impl() const {
3173 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003174}
3175
Devang Patel11cf3f42009-10-27 22:16:29 +00003176AllocaInst *AllocaInst::clone_impl() const {
3177 return new AllocaInst(getAllocatedType(),
3178 (Value*)getOperand(0),
3179 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003180}
3181
Devang Patel11cf3f42009-10-27 22:16:29 +00003182LoadInst *LoadInst::clone_impl() const {
3183 return new LoadInst(getOperand(0),
3184 Twine(), isVolatile(),
3185 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003186}
3187
Devang Patel11cf3f42009-10-27 22:16:29 +00003188StoreInst *StoreInst::clone_impl() const {
3189 return new StoreInst(getOperand(0), getOperand(1),
3190 isVolatile(), getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003191}
3192
Devang Patel11cf3f42009-10-27 22:16:29 +00003193TruncInst *TruncInst::clone_impl() const {
3194 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003195}
3196
Devang Patel11cf3f42009-10-27 22:16:29 +00003197ZExtInst *ZExtInst::clone_impl() const {
3198 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003199}
3200
Devang Patel11cf3f42009-10-27 22:16:29 +00003201SExtInst *SExtInst::clone_impl() const {
3202 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003203}
3204
Devang Patel11cf3f42009-10-27 22:16:29 +00003205FPTruncInst *FPTruncInst::clone_impl() const {
3206 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003207}
3208
Devang Patel11cf3f42009-10-27 22:16:29 +00003209FPExtInst *FPExtInst::clone_impl() const {
3210 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003211}
3212
Devang Patel11cf3f42009-10-27 22:16:29 +00003213UIToFPInst *UIToFPInst::clone_impl() const {
3214 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003215}
3216
Devang Patel11cf3f42009-10-27 22:16:29 +00003217SIToFPInst *SIToFPInst::clone_impl() const {
3218 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003219}
3220
Devang Patel11cf3f42009-10-27 22:16:29 +00003221FPToUIInst *FPToUIInst::clone_impl() const {
3222 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003223}
3224
Devang Patel11cf3f42009-10-27 22:16:29 +00003225FPToSIInst *FPToSIInst::clone_impl() const {
3226 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003227}
3228
Devang Patel11cf3f42009-10-27 22:16:29 +00003229PtrToIntInst *PtrToIntInst::clone_impl() const {
3230 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003231}
3232
Devang Patel11cf3f42009-10-27 22:16:29 +00003233IntToPtrInst *IntToPtrInst::clone_impl() const {
3234 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003235}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003236
Devang Patel11cf3f42009-10-27 22:16:29 +00003237BitCastInst *BitCastInst::clone_impl() const {
3238 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003239}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003240
Devang Patel11cf3f42009-10-27 22:16:29 +00003241CallInst *CallInst::clone_impl() const {
3242 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003243}
3244
Devang Patel11cf3f42009-10-27 22:16:29 +00003245SelectInst *SelectInst::clone_impl() const {
3246 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003247}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003248
Devang Patel11cf3f42009-10-27 22:16:29 +00003249VAArgInst *VAArgInst::clone_impl() const {
3250 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003251}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003252
Devang Patel11cf3f42009-10-27 22:16:29 +00003253ExtractElementInst *ExtractElementInst::clone_impl() const {
3254 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003255}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003256
Devang Patel11cf3f42009-10-27 22:16:29 +00003257InsertElementInst *InsertElementInst::clone_impl() const {
3258 return InsertElementInst::Create(getOperand(0),
3259 getOperand(1),
3260 getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003261}
3262
Devang Patel11cf3f42009-10-27 22:16:29 +00003263ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3264 return new ShuffleVectorInst(getOperand(0),
3265 getOperand(1),
3266 getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003267}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003268
Devang Patel11cf3f42009-10-27 22:16:29 +00003269PHINode *PHINode::clone_impl() const {
3270 return new PHINode(*this);
3271}
3272
3273ReturnInst *ReturnInst::clone_impl() const {
3274 return new(getNumOperands()) ReturnInst(*this);
3275}
3276
3277BranchInst *BranchInst::clone_impl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003278 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003279}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003280
Devang Patel11cf3f42009-10-27 22:16:29 +00003281SwitchInst *SwitchInst::clone_impl() const {
3282 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003283}
3284
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003285IndirectBrInst *IndirectBrInst::clone_impl() const {
3286 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003287}
3288
3289
Devang Patel11cf3f42009-10-27 22:16:29 +00003290InvokeInst *InvokeInst::clone_impl() const {
3291 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003292}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003293
Devang Patel11cf3f42009-10-27 22:16:29 +00003294UnwindInst *UnwindInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003295 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003296 return new UnwindInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003297}
3298
Devang Patel11cf3f42009-10-27 22:16:29 +00003299UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003300 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003301 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003302}