blob: 8f4eabeb8aeeb2e10f95c4e6231a58989ab77957 [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()) {
Eric Christopher96513122011-06-23 06:24:52 +000090 Use *OL = OperandList;
91 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
92 OL[i] = PN.getOperand(i);
93 OL[i+1] = PN.getOperand(i+1);
94 }
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) {
Eric Christopher96513122011-06-23 06:24:52 +0000105 unsigned NumOps = getNumOperands();
106 Use *OL = OperandList;
107 assert(Idx*2 < NumOps && "BB not in PHI node!");
108 Value *Removed = OL[Idx*2];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000109
110 // Move everything after this operand down.
111 //
112 // FIXME: we could just swap with the end of the list, then erase. However,
Eric Christopher96513122011-06-23 06:24:52 +0000113 // client might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000114 // use/def lists, which is kinda lame.
Eric Christopher96513122011-06-23 06:24:52 +0000115 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 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000119
120 // Nuke the last value.
Eric Christopher96513122011-06-23 06:24:52 +0000121 OL[NumOps-2].set(0);
122 OL[NumOps-2+1].set(0);
123 NumOperands = NumOps-2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000124
125 // If the PHI node is dead, because it has zero entries, nuke it now.
Eric Christopher96513122011-06-23 06:24:52 +0000126 if (NumOps == 2 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000127 // 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();
Eric Christopher96513122011-06-23 06:24:52 +0000140 // Multiply by 1.5 and round down so the result is still even.
141 unsigned NumOps = e + e / 4 * 2;
142 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
Jay Foade03c05c2011-06-20 14:38:01 +0000143
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000144 ReservedSpace = NumOps;
Eric Christopher96513122011-06-23 06:24:52 +0000145 Use *OldOps = OperandList;
146 Use *NewOps = allocHungoffUses(NumOps);
147 std::copy(OldOps, OldOps + e, NewOps);
148 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +0000149 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000150}
151
Nate Begemanb3923212005-08-04 23:24:19 +0000152/// hasConstantValue - If the specified PHI node always merges together the same
153/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000154Value *PHINode::hasConstantValue() const {
155 // Exploit the fact that phi nodes always have at least one entry.
156 Value *ConstantValue = getIncomingValue(0);
157 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
158 if (getIncomingValue(i) != ConstantValue)
159 return 0; // Incoming values not all the same.
160 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000161}
162
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000163
164//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000165// CallInst Implementation
166//===----------------------------------------------------------------------===//
167
Gordon Henriksen14a55692007-12-10 02:14:30 +0000168CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000169}
170
Chris Lattner054ba2c2007-02-13 00:58:44 +0000171void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000172 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000173 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000174
Misha Brukmanb1c93172005-04-21 23:48:37 +0000175 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000176 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000177 (void)FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000178
Chris Lattner054ba2c2007-02-13 00:58:44 +0000179 assert((NumParams == FTy->getNumParams() ||
180 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000181 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000182 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000183 assert((i >= FTy->getNumParams() ||
184 FTy->getParamType(i) == Params[i]->getType()) &&
185 "Calling a function with a bad signature!");
Gabor Greif6d673952010-07-16 09:38:02 +0000186 OperandList[i] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000187 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000188}
189
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000190void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000191 assert(NumOperands == 3 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000192 Op<-1>() = Func;
193 Op<0>() = Actual1;
194 Op<1>() = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000195
196 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000197 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000198 (void)FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000199
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000200 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000201 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000202 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000203 assert((0 >= FTy->getNumParams() ||
204 FTy->getParamType(0) == Actual1->getType()) &&
205 "Calling a function with a bad signature!");
206 assert((1 >= FTy->getNumParams() ||
207 FTy->getParamType(1) == Actual2->getType()) &&
208 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000209}
210
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000211void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000212 assert(NumOperands == 2 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000213 Op<-1>() = Func;
214 Op<0>() = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000215
216 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000217 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000218 (void)FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000219
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000220 assert((FTy->getNumParams() == 1 ||
221 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000222 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000223 assert((0 == FTy->getNumParams() ||
224 FTy->getParamType(0) == Actual->getType()) &&
225 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000226}
227
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000228void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000229 assert(NumOperands == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000230 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000231
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000232 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000233 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000234 (void)FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000235
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000236 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000237}
238
Daniel Dunbar4975db62009-07-25 04:41:11 +0000239CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000240 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000241 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
242 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000243 Instruction::Call,
244 OperandTraits<CallInst>::op_end(this) - 2,
245 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000246 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000247 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000248}
249
Daniel Dunbar4975db62009-07-25 04:41:11 +0000250CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000251 BasicBlock *InsertAtEnd)
252 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
253 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000254 Instruction::Call,
255 OperandTraits<CallInst>::op_end(this) - 2,
256 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000257 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000258 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000259}
Daniel Dunbar4975db62009-07-25 04:41:11 +0000260CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000261 Instruction *InsertBefore)
262 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
263 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000264 Instruction::Call,
265 OperandTraits<CallInst>::op_end(this) - 1,
266 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000267 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000268 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000269}
270
Daniel Dunbar4975db62009-07-25 04:41:11 +0000271CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000272 BasicBlock *InsertAtEnd)
273 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
274 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000275 Instruction::Call,
276 OperandTraits<CallInst>::op_end(this) - 1,
277 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000278 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000279 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000280}
281
Misha Brukmanb1c93172005-04-21 23:48:37 +0000282CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000283 : Instruction(CI.getType(), Instruction::Call,
284 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000285 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000286 setAttributes(CI.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000287 setTailCall(CI.isTailCall());
288 setCallingConv(CI.getCallingConv());
289
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000290 Use *OL = OperandList;
291 Use *InOL = CI.OperandList;
292 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000293 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000294 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000295}
296
Devang Patel4c758ea2008-09-25 21:00:45 +0000297void CallInst::addAttribute(unsigned i, Attributes attr) {
298 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000299 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000300 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000301}
302
Devang Patel4c758ea2008-09-25 21:00:45 +0000303void CallInst::removeAttribute(unsigned i, Attributes attr) {
304 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000305 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000306 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000307}
308
Devang Patelba3fa6c2008-09-23 23:03:40 +0000309bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000310 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000311 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000312 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000313 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000314 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000315}
316
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000317/// IsConstantOne - Return true only if val is constant int 1
318static bool IsConstantOne(Value *val) {
319 assert(val && "IsConstantOne does not work with NULL val");
320 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
321}
322
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000323static Instruction *createMalloc(Instruction *InsertBefore,
324 BasicBlock *InsertAtEnd, const Type *IntPtrTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000325 const Type *AllocTy, Value *AllocSize,
326 Value *ArraySize, Function *MallocF,
327 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000328 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000329 "createMalloc needs either InsertBefore or InsertAtEnd");
330
331 // malloc(type) becomes:
332 // bitcast (i8* malloc(typeSize)) to type*
333 // malloc(type, arraySize) becomes:
334 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000335 if (!ArraySize)
336 ArraySize = ConstantInt::get(IntPtrTy, 1);
337 else if (ArraySize->getType() != IntPtrTy) {
338 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000339 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
340 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000341 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000342 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
343 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000344 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000345
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000346 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000347 if (IsConstantOne(AllocSize)) {
348 AllocSize = ArraySize; // Operand * 1 = Operand
349 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
350 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
351 false /*ZExt*/);
352 // Malloc arg is constant product of type size and array size
353 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
354 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000355 // Multiply type size by the array size...
356 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000357 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
358 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000359 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000360 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
361 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000362 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000363 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000364
Victor Hernandez788eaab2009-09-18 19:20:02 +0000365 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000366 // Create the call to Malloc.
367 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
368 Module* M = BB->getParent()->getParent();
Duncan Sands9ed7b162009-10-06 15:40:36 +0000369 const Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000370 Value *MallocFunc = MallocF;
371 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000372 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000373 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000374 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000375 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000376 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000377 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000378 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000379 Result = MCall;
380 if (Result->getType() != AllocPtrType)
381 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000382 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000383 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000384 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000385 Result = MCall;
386 if (Result->getType() != AllocPtrType) {
387 InsertAtEnd->getInstList().push_back(MCall);
388 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000389 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000390 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000391 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000392 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000393 if (Function *F = dyn_cast<Function>(MallocFunc)) {
394 MCall->setCallingConv(F->getCallingConv());
395 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
396 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000397 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000398
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000399 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000400}
401
402/// CreateMalloc - Generate the IR for a call to malloc:
403/// 1. Compute the malloc call's argument as the specified type's size,
404/// possibly multiplied by the array size if the array size is not
405/// constant 1.
406/// 2. Call malloc with that argument.
407/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000408Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
409 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000410 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000411 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000412 const Twine &Name) {
413 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000414 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000415}
416
417/// CreateMalloc - Generate the IR for a call to malloc:
418/// 1. Compute the malloc call's argument as the specified type's size,
419/// possibly multiplied by the array size if the array size is not
420/// constant 1.
421/// 2. Call malloc with that argument.
422/// 3. Bitcast the result of the malloc call to the specified type.
423/// Note: This function does not add the bitcast to the basic block, that is the
424/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000425Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
426 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000427 Value *AllocSize, Value *ArraySize,
428 Function *MallocF, const Twine &Name) {
429 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000430 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000431}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000432
Victor Hernandeze2971492009-10-24 04:23:03 +0000433static Instruction* createFree(Value* Source, Instruction *InsertBefore,
434 BasicBlock *InsertAtEnd) {
435 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
436 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000437 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000438 "Can not free something of nonpointer type!");
439
440 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
441 Module* M = BB->getParent()->getParent();
442
443 const Type *VoidTy = Type::getVoidTy(M->getContext());
444 const Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
445 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000446 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000447 CallInst* Result = NULL;
448 Value *PtrCast = Source;
449 if (InsertBefore) {
450 if (Source->getType() != IntPtrTy)
451 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
452 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
453 } else {
454 if (Source->getType() != IntPtrTy)
455 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
456 Result = CallInst::Create(FreeFunc, PtrCast, "");
457 }
458 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000459 if (Function *F = dyn_cast<Function>(FreeFunc))
460 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000461
462 return Result;
463}
464
465/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000466Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
467 return createFree(Source, InsertBefore, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000468}
469
470/// CreateFree - Generate the IR for a call to the builtin free function.
471/// Note: This function does not add the call to the basic block, that is the
472/// responsibility of the caller.
473Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
474 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
475 assert(FreeCall && "CreateFree did not create a CallInst");
476 return FreeCall;
477}
478
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000479//===----------------------------------------------------------------------===//
480// InvokeInst Implementation
481//===----------------------------------------------------------------------===//
482
483void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000484 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000485 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000486 Op<-3>() = Fn;
487 Op<-2>() = IfNormal;
488 Op<-1>() = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000489 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000490 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000491 (void)FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000492
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000493 assert(((NumArgs == FTy->getNumParams()) ||
494 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000495 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000496
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000497 Use *OL = OperandList;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000498 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000499 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000500 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000501 "Invoking a function with a bad signature!");
502
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000503 OL[i] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000504 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000505}
506
Misha Brukmanb1c93172005-04-21 23:48:37 +0000507InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000508 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000509 OperandTraits<InvokeInst>::op_end(this)
510 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000511 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000512 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000513 setCallingConv(II.getCallingConv());
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000514 Use *OL = OperandList, *InOL = II.OperandList;
515 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000516 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000517 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000518}
519
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000520BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
521 return getSuccessor(idx);
522}
523unsigned InvokeInst::getNumSuccessorsV() const {
524 return getNumSuccessors();
525}
526void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
527 return setSuccessor(idx, B);
528}
529
Devang Patelba3fa6c2008-09-23 23:03:40 +0000530bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000531 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000532 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000533 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000534 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000535 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000536}
537
Devang Patel4c758ea2008-09-25 21:00:45 +0000538void InvokeInst::addAttribute(unsigned i, Attributes attr) {
539 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000540 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000541 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000542}
543
Devang Patel4c758ea2008-09-25 21:00:45 +0000544void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
545 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000546 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000547 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000548}
549
Duncan Sands5208d1a2007-11-28 17:07:01 +0000550
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000551//===----------------------------------------------------------------------===//
552// ReturnInst Implementation
553//===----------------------------------------------------------------------===//
554
Chris Lattner2195fc42007-02-24 00:55:48 +0000555ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000556 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000557 OperandTraits<ReturnInst>::op_end(this) -
558 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000559 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000560 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000561 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000562 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000563}
564
Owen Anderson55f1c092009-08-13 21:58:54 +0000565ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
566 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000567 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
568 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000569 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000570 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000571}
Owen Anderson55f1c092009-08-13 21:58:54 +0000572ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
573 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000574 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
575 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000576 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000577 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000578}
Owen Anderson55f1c092009-08-13 21:58:54 +0000579ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
580 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000581 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000582}
583
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000584unsigned ReturnInst::getNumSuccessorsV() const {
585 return getNumSuccessors();
586}
587
Devang Patelae682fb2008-02-26 17:56:20 +0000588/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
589/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000590void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000591 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000592}
593
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000594BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000595 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000596 return 0;
597}
598
Devang Patel59643e52008-02-23 00:35:18 +0000599ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000600}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000601
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000602//===----------------------------------------------------------------------===//
603// UnwindInst Implementation
604//===----------------------------------------------------------------------===//
605
Owen Anderson55f1c092009-08-13 21:58:54 +0000606UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
607 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
608 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000609}
Owen Anderson55f1c092009-08-13 21:58:54 +0000610UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
611 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
612 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000613}
614
615
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000616unsigned UnwindInst::getNumSuccessorsV() const {
617 return getNumSuccessors();
618}
619
620void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000621 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000622}
623
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000624BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000625 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000626 return 0;
627}
628
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000629//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000630// UnreachableInst Implementation
631//===----------------------------------------------------------------------===//
632
Owen Anderson55f1c092009-08-13 21:58:54 +0000633UnreachableInst::UnreachableInst(LLVMContext &Context,
634 Instruction *InsertBefore)
635 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
636 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000637}
Owen Anderson55f1c092009-08-13 21:58:54 +0000638UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
639 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
640 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000641}
642
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000643unsigned UnreachableInst::getNumSuccessorsV() const {
644 return getNumSuccessors();
645}
646
647void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000648 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000649}
650
651BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000652 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000653 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000654}
655
656//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000657// BranchInst Implementation
658//===----------------------------------------------------------------------===//
659
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000660void BranchInst::AssertOK() {
661 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000662 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000663 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000664}
665
Chris Lattner2195fc42007-02-24 00:55:48 +0000666BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000667 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000668 OperandTraits<BranchInst>::op_end(this) - 1,
669 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000670 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000671 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000672}
673BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
674 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000675 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000676 OperandTraits<BranchInst>::op_end(this) - 3,
677 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000678 Op<-1>() = IfTrue;
679 Op<-2>() = IfFalse;
680 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000681#ifndef NDEBUG
682 AssertOK();
683#endif
684}
685
686BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000687 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000688 OperandTraits<BranchInst>::op_end(this) - 1,
689 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000690 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000691 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000692}
693
694BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
695 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000696 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000697 OperandTraits<BranchInst>::op_end(this) - 3,
698 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000699 Op<-1>() = IfTrue;
700 Op<-2>() = IfFalse;
701 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000702#ifndef NDEBUG
703 AssertOK();
704#endif
705}
706
707
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000708BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000709 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000710 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
711 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000712 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000713 if (BI.getNumOperands() != 1) {
714 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000715 Op<-3>() = BI.Op<-3>();
716 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000717 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000718 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000719}
720
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000721BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
722 return getSuccessor(idx);
723}
724unsigned BranchInst::getNumSuccessorsV() const {
725 return getNumSuccessors();
726}
727void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
728 setSuccessor(idx, B);
729}
730
731
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000732//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000733// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000734//===----------------------------------------------------------------------===//
735
Owen Andersonb6b25302009-07-14 23:09:55 +0000736static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000737 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000738 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000739 else {
740 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000741 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000742 assert(Amt->getType()->isIntegerTy() &&
743 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000744 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000745 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000746}
747
Victor Hernandez8acf2952009-10-23 21:09:37 +0000748AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
749 const Twine &Name, Instruction *InsertBefore)
750 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
751 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
752 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000753 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000754 setName(Name);
755}
756
757AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
758 const Twine &Name, BasicBlock *InsertAtEnd)
759 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
760 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
761 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000762 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000763 setName(Name);
764}
765
766AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
767 Instruction *InsertBefore)
768 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
769 getAISize(Ty->getContext(), 0), InsertBefore) {
770 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000771 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000772 setName(Name);
773}
774
775AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
776 BasicBlock *InsertAtEnd)
777 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
778 getAISize(Ty->getContext(), 0), InsertAtEnd) {
779 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000780 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000781 setName(Name);
782}
783
784AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
785 const Twine &Name, Instruction *InsertBefore)
786 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000787 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000788 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000789 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000790 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000791}
792
Victor Hernandez8acf2952009-10-23 21:09:37 +0000793AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
794 const Twine &Name, BasicBlock *InsertAtEnd)
795 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000796 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000797 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000798 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000799 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000800}
801
Gordon Henriksen14a55692007-12-10 02:14:30 +0000802// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000803AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000804}
805
Victor Hernandez8acf2952009-10-23 21:09:37 +0000806void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000807 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000808 assert(Align <= MaximumAlignment &&
809 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000810 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000811 assert(getAlignment() == Align && "Alignment representation error!");
812}
813
Victor Hernandez8acf2952009-10-23 21:09:37 +0000814bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000815 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +0000816 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000817 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000818}
819
Victor Hernandez8acf2952009-10-23 21:09:37 +0000820const Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000821 return getType()->getElementType();
822}
823
Chris Lattner8b291e62008-11-26 02:54:17 +0000824/// isStaticAlloca - Return true if this alloca is in the entry block of the
825/// function and is a constant size. If so, the code generator will fold it
826/// into the prolog/epilog code, so it is basically free.
827bool AllocaInst::isStaticAlloca() const {
828 // Must be constant size.
829 if (!isa<ConstantInt>(getArraySize())) return false;
830
831 // Must be in the entry block.
832 const BasicBlock *Parent = getParent();
833 return Parent == &Parent->getParent()->front();
834}
835
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000836//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000837// LoadInst Implementation
838//===----------------------------------------------------------------------===//
839
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000840void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000841 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000842 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000843}
844
Daniel Dunbar4975db62009-07-25 04:41:11 +0000845LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000846 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000847 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000848 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000849 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000850 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000851 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000852}
853
Daniel Dunbar4975db62009-07-25 04:41:11 +0000854LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000855 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000856 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000857 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000858 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000859 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000860 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000861}
862
Daniel Dunbar4975db62009-07-25 04:41:11 +0000863LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000864 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000865 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000866 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000867 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000868 setAlignment(0);
869 AssertOK();
870 setName(Name);
871}
872
Daniel Dunbar4975db62009-07-25 04:41:11 +0000873LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000874 unsigned Align, Instruction *InsertBef)
875 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
876 Load, Ptr, InsertBef) {
877 setVolatile(isVolatile);
878 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000879 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000880 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000881}
882
Daniel Dunbar4975db62009-07-25 04:41:11 +0000883LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000884 unsigned Align, BasicBlock *InsertAE)
885 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
886 Load, Ptr, InsertAE) {
887 setVolatile(isVolatile);
888 setAlignment(Align);
889 AssertOK();
890 setName(Name);
891}
892
Daniel Dunbar4975db62009-07-25 04:41:11 +0000893LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000894 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000895 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000896 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000897 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000898 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000899 AssertOK();
900 setName(Name);
901}
902
Daniel Dunbar27096822009-08-11 18:11:15 +0000903
904
905LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
906 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
907 Load, Ptr, InsertBef) {
908 setVolatile(false);
909 setAlignment(0);
910 AssertOK();
911 if (Name && Name[0]) setName(Name);
912}
913
914LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
915 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
916 Load, Ptr, InsertAE) {
917 setVolatile(false);
918 setAlignment(0);
919 AssertOK();
920 if (Name && Name[0]) setName(Name);
921}
922
923LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
924 Instruction *InsertBef)
925: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
926 Load, Ptr, InsertBef) {
927 setVolatile(isVolatile);
928 setAlignment(0);
929 AssertOK();
930 if (Name && Name[0]) setName(Name);
931}
932
933LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
934 BasicBlock *InsertAE)
935 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
936 Load, Ptr, InsertAE) {
937 setVolatile(isVolatile);
938 setAlignment(0);
939 AssertOK();
940 if (Name && Name[0]) setName(Name);
941}
942
Christopher Lamb84485702007-04-22 19:24:39 +0000943void LoadInst::setAlignment(unsigned Align) {
944 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000945 assert(Align <= MaximumAlignment &&
946 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000947 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
948 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +0000949 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +0000950}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000951
952//===----------------------------------------------------------------------===//
953// StoreInst Implementation
954//===----------------------------------------------------------------------===//
955
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000956void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +0000957 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +0000958 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000959 "Ptr must have pointer type!");
960 assert(getOperand(0)->getType() ==
961 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000962 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000963}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000964
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000965
966StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000967 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000968 OperandTraits<StoreInst>::op_begin(this),
969 OperandTraits<StoreInst>::operands(this),
970 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000971 Op<0>() = val;
972 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000973 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000974 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000975 AssertOK();
976}
977
978StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000979 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000980 OperandTraits<StoreInst>::op_begin(this),
981 OperandTraits<StoreInst>::operands(this),
982 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000983 Op<0>() = val;
984 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000985 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000986 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000987 AssertOK();
988}
989
Misha Brukmanb1c93172005-04-21 23:48:37 +0000990StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000991 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000992 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000993 OperandTraits<StoreInst>::op_begin(this),
994 OperandTraits<StoreInst>::operands(this),
995 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000996 Op<0>() = val;
997 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000998 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000999 setAlignment(0);
1000 AssertOK();
1001}
1002
1003StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1004 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001005 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001006 OperandTraits<StoreInst>::op_begin(this),
1007 OperandTraits<StoreInst>::operands(this),
1008 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001009 Op<0>() = val;
1010 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001011 setVolatile(isVolatile);
1012 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001013 AssertOK();
1014}
1015
Misha Brukmanb1c93172005-04-21 23:48:37 +00001016StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001017 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001018 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001019 OperandTraits<StoreInst>::op_begin(this),
1020 OperandTraits<StoreInst>::operands(this),
1021 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001022 Op<0>() = val;
1023 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001024 setVolatile(isVolatile);
1025 setAlignment(Align);
1026 AssertOK();
1027}
1028
1029StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001030 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001031 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001032 OperandTraits<StoreInst>::op_begin(this),
1033 OperandTraits<StoreInst>::operands(this),
1034 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001035 Op<0>() = val;
1036 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001037 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001038 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001039 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001040}
1041
Christopher Lamb84485702007-04-22 19:24:39 +00001042void StoreInst::setAlignment(unsigned Align) {
1043 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001044 assert(Align <= MaximumAlignment &&
1045 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001046 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1047 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001048 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001049}
1050
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001051//===----------------------------------------------------------------------===//
1052// GetElementPtrInst Implementation
1053//===----------------------------------------------------------------------===//
1054
Christopher Lambedf07882007-12-17 01:12:55 +00001055static unsigned retrieveAddrSpace(const Value *Val) {
1056 return cast<PointerType>(Val->getType())->getAddressSpace();
1057}
1058
Gabor Greif21ba1842008-06-06 20:28:12 +00001059void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001060 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001061 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1062 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001063 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001064
Chris Lattner79807c3d2007-01-31 19:47:18 +00001065 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001066 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001067
1068 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001069}
1070
Daniel Dunbar4975db62009-07-25 04:41:11 +00001071void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001072 assert(NumOperands == 2 && "NumOperands not initialized?");
1073 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001074 OL[0] = Ptr;
1075 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001076
1077 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001078}
1079
Gabor Greiff6caff662008-05-10 08:32:32 +00001080GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001081 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001082 OperandTraits<GetElementPtrInst>::op_end(this)
1083 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001084 GEPI.getNumOperands()) {
1085 Use *OL = OperandList;
1086 Use *GEPIOL = GEPI.OperandList;
1087 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001088 OL[i] = GEPIOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001089 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001090}
1091
Chris Lattner82981202005-05-03 05:43:30 +00001092GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001093 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001094 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001095 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001096 GetElementPtr,
1097 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1098 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001099 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001100}
1101
1102GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001103 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001104 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001105 checkType(getIndexedType(Ptr->getType(),Idx)),
1106 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001107 GetElementPtr,
1108 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1109 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001110 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001111}
1112
Chris Lattner6090a422009-03-09 04:46:40 +00001113/// getIndexedType - Returns the type of the element that would be accessed with
1114/// a gep instruction with the specified parameters.
1115///
1116/// The Idxs pointer should point to a continuous piece of memory containing the
1117/// indices, either as Value* or uint64_t.
1118///
1119/// A null type is returned if the indices are invalid for the specified
1120/// pointer type.
1121///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001122template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001123static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1124 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001125 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1126 if (!PTy) return 0; // Type isn't a pointer type!
1127 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001128
Chris Lattner6090a422009-03-09 04:46:40 +00001129 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001130 if (NumIdx == 0)
1131 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001132
1133 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001134 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1135 // that contain opaque types) under the assumption that it will be resolved to
1136 // a sane type later.
1137 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001138 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001139
Dan Gohman1ecaf452008-05-31 00:58:22 +00001140 unsigned CurIdx = 1;
1141 for (; CurIdx != NumIdx; ++CurIdx) {
1142 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001143 if (!CT || CT->isPointerTy()) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001144 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001145 if (!CT->indexValid(Index)) return 0;
1146 Agg = CT->getTypeAtIndex(Index);
1147
1148 // If the new type forwards to another type, then it is in the middle
1149 // of being refined to another type (and hence, may have dropped all
1150 // references to what it was using before). So, use the new forwarded
1151 // type.
1152 if (const Type *Ty = Agg->getForwardedType())
1153 Agg = Ty;
1154 }
1155 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001156}
1157
Matthijs Kooijman04468622008-07-29 08:46:11 +00001158const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1159 Value* const *Idxs,
1160 unsigned NumIdx) {
1161 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1162}
1163
1164const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001165 Constant* const *Idxs,
1166 unsigned NumIdx) {
1167 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1168}
1169
1170const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Matthijs Kooijman04468622008-07-29 08:46:11 +00001171 uint64_t const *Idxs,
1172 unsigned NumIdx) {
1173 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1174}
1175
Chris Lattner82981202005-05-03 05:43:30 +00001176const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1177 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1178 if (!PTy) return 0; // Type isn't a pointer type!
1179
1180 // Check the pointer index.
1181 if (!PTy->indexValid(Idx)) return 0;
1182
Chris Lattnerc2233332005-05-03 16:44:45 +00001183 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001184}
1185
Chris Lattner45f15572007-04-14 00:12:57 +00001186
1187/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1188/// zeros. If so, the result pointer and the first operand have the same
1189/// value, just potentially different types.
1190bool GetElementPtrInst::hasAllZeroIndices() const {
1191 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1192 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1193 if (!CI->isZero()) return false;
1194 } else {
1195 return false;
1196 }
1197 }
1198 return true;
1199}
1200
Chris Lattner27058292007-04-27 20:35:56 +00001201/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1202/// constant integers. If so, the result pointer and the first operand have
1203/// a constant offset between them.
1204bool GetElementPtrInst::hasAllConstantIndices() const {
1205 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1206 if (!isa<ConstantInt>(getOperand(i)))
1207 return false;
1208 }
1209 return true;
1210}
1211
Dan Gohman1b849082009-09-07 23:54:19 +00001212void GetElementPtrInst::setIsInBounds(bool B) {
1213 cast<GEPOperator>(this)->setIsInBounds(B);
1214}
Chris Lattner45f15572007-04-14 00:12:57 +00001215
Nick Lewycky28a5f252009-09-27 21:33:04 +00001216bool GetElementPtrInst::isInBounds() const {
1217 return cast<GEPOperator>(this)->isInBounds();
1218}
1219
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001220//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001221// ExtractElementInst Implementation
1222//===----------------------------------------------------------------------===//
1223
1224ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001225 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001226 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001227 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001228 ExtractElement,
1229 OperandTraits<ExtractElementInst>::op_begin(this),
1230 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001231 assert(isValidOperands(Val, Index) &&
1232 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001233 Op<0>() = Val;
1234 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001235 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001236}
1237
1238ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001239 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001240 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001241 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001242 ExtractElement,
1243 OperandTraits<ExtractElementInst>::op_begin(this),
1244 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001245 assert(isValidOperands(Val, Index) &&
1246 "Invalid extractelement instruction operands!");
1247
Gabor Greif2d3024d2008-05-26 21:33:52 +00001248 Op<0>() = Val;
1249 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001250 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001251}
1252
Chris Lattner65511ff2006-10-05 06:24:58 +00001253
Chris Lattner54865b32006-04-08 04:05:48 +00001254bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001255 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001256 return false;
1257 return true;
1258}
1259
1260
Robert Bocchino23004482006-01-10 19:05:34 +00001261//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001262// InsertElementInst Implementation
1263//===----------------------------------------------------------------------===//
1264
Chris Lattner54865b32006-04-08 04:05:48 +00001265InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001266 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001267 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001268 : Instruction(Vec->getType(), InsertElement,
1269 OperandTraits<InsertElementInst>::op_begin(this),
1270 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001271 assert(isValidOperands(Vec, Elt, Index) &&
1272 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001273 Op<0>() = Vec;
1274 Op<1>() = Elt;
1275 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001276 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001277}
1278
Chris Lattner54865b32006-04-08 04:05:48 +00001279InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001280 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001281 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001282 : Instruction(Vec->getType(), InsertElement,
1283 OperandTraits<InsertElementInst>::op_begin(this),
1284 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001285 assert(isValidOperands(Vec, Elt, Index) &&
1286 "Invalid insertelement instruction operands!");
1287
Gabor Greif2d3024d2008-05-26 21:33:52 +00001288 Op<0>() = Vec;
1289 Op<1>() = Elt;
1290 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001291 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001292}
1293
Chris Lattner54865b32006-04-08 04:05:48 +00001294bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1295 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001296 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001297 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001298
Reid Spencerd84d35b2007-02-15 02:26:10 +00001299 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001300 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001301
Duncan Sands9dff9be2010-02-15 16:12:20 +00001302 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001303 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001304 return true;
1305}
1306
1307
Robert Bocchinoca27f032006-01-17 20:07:22 +00001308//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001309// ShuffleVectorInst Implementation
1310//===----------------------------------------------------------------------===//
1311
1312ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001313 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001314 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001315: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001316 cast<VectorType>(Mask->getType())->getNumElements()),
1317 ShuffleVector,
1318 OperandTraits<ShuffleVectorInst>::op_begin(this),
1319 OperandTraits<ShuffleVectorInst>::operands(this),
1320 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001321 assert(isValidOperands(V1, V2, Mask) &&
1322 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001323 Op<0>() = V1;
1324 Op<1>() = V2;
1325 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001326 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001327}
1328
1329ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001330 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001331 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001332: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1333 cast<VectorType>(Mask->getType())->getNumElements()),
1334 ShuffleVector,
1335 OperandTraits<ShuffleVectorInst>::op_begin(this),
1336 OperandTraits<ShuffleVectorInst>::operands(this),
1337 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001338 assert(isValidOperands(V1, V2, Mask) &&
1339 "Invalid shuffle vector instruction operands!");
1340
Gabor Greif2d3024d2008-05-26 21:33:52 +00001341 Op<0>() = V1;
1342 Op<1>() = V2;
1343 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001344 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001345}
1346
Mon P Wang25f01062008-11-10 04:46:22 +00001347bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001348 const Value *Mask) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001349 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001350 return false;
1351
1352 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001353 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001354 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001355
1356 // Check to see if Mask is valid.
1357 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
1358 const VectorType *VTy = cast<VectorType>(V1->getType());
1359 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
1360 if (ConstantInt* CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1361 if (CI->uge(VTy->getNumElements()*2))
1362 return false;
1363 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1364 return false;
1365 }
1366 }
1367 }
1368 else if (!isa<UndefValue>(Mask) && !isa<ConstantAggregateZero>(Mask))
1369 return false;
1370
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001371 return true;
1372}
1373
Chris Lattnerf724e342008-03-02 05:28:33 +00001374/// getMaskValue - Return the index from the shuffle mask for the specified
1375/// output result. This is either -1 if the element is undef or a number less
1376/// than 2*numelements.
1377int ShuffleVectorInst::getMaskValue(unsigned i) const {
1378 const Constant *Mask = cast<Constant>(getOperand(2));
1379 if (isa<UndefValue>(Mask)) return -1;
1380 if (isa<ConstantAggregateZero>(Mask)) return 0;
1381 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1382 assert(i < MaskCV->getNumOperands() && "Index out of range");
1383
1384 if (isa<UndefValue>(MaskCV->getOperand(i)))
1385 return -1;
1386 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1387}
1388
Dan Gohman12fce772008-05-15 19:50:34 +00001389//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001390// InsertValueInst Class
1391//===----------------------------------------------------------------------===//
1392
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001393void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001394 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001395 assert(NumOperands == 2 && "NumOperands not initialized?");
Frits van Bommel16ebe772010-12-05 20:50:26 +00001396 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idx, Idx + NumIdx) ==
1397 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001398 Op<0>() = Agg;
1399 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001400
Dan Gohmandd41bba2010-06-21 19:47:52 +00001401 Indices.append(Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001402 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001403}
1404
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001405void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001406 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001407 assert(NumOperands == 2 && "NumOperands not initialized?");
Frits van Bommel16ebe772010-12-05 20:50:26 +00001408 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idx) == Val->getType()
1409 && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001410 Op<0>() = Agg;
1411 Op<1>() = Val;
1412
1413 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001414 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001415}
1416
1417InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001418 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001419 OperandTraits<InsertValueInst>::op_begin(this), 2),
1420 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001421 Op<0>() = IVI.getOperand(0);
1422 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001423 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001424}
1425
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001426InsertValueInst::InsertValueInst(Value *Agg,
1427 Value *Val,
1428 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001429 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001430 Instruction *InsertBefore)
1431 : Instruction(Agg->getType(), InsertValue,
1432 OperandTraits<InsertValueInst>::op_begin(this),
1433 2, InsertBefore) {
1434 init(Agg, Val, Idx, Name);
1435}
1436
1437InsertValueInst::InsertValueInst(Value *Agg,
1438 Value *Val,
1439 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001440 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001441 BasicBlock *InsertAtEnd)
1442 : Instruction(Agg->getType(), InsertValue,
1443 OperandTraits<InsertValueInst>::op_begin(this),
1444 2, InsertAtEnd) {
1445 init(Agg, Val, Idx, Name);
1446}
1447
Dan Gohman0752bff2008-05-23 00:36:11 +00001448//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001449// ExtractValueInst Class
1450//===----------------------------------------------------------------------===//
1451
Gabor Greifcbcc4952008-06-06 21:06:32 +00001452void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001453 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001454 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001455
Dan Gohmandd41bba2010-06-21 19:47:52 +00001456 Indices.append(Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001457 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001458}
1459
Daniel Dunbar4975db62009-07-25 04:41:11 +00001460void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001461 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001462
1463 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001464 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001465}
1466
1467ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001468 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001469 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001470 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001471}
1472
Dan Gohman12fce772008-05-15 19:50:34 +00001473// getIndexedType - Returns the type of the element that would be extracted
1474// with an extractvalue instruction with the specified parameters.
1475//
1476// A null type is returned if the indices are invalid for the specified
1477// pointer type.
1478//
1479const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001480 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001481 unsigned NumIdx) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001482 for (unsigned CurIdx = 0; CurIdx != NumIdx; ++CurIdx) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001483 unsigned Index = Idxs[CurIdx];
Frits van Bommel16ebe772010-12-05 20:50:26 +00001484 // We can't use CompositeType::indexValid(Index) here.
1485 // indexValid() always returns true for arrays because getelementptr allows
1486 // out-of-bounds indices. Since we don't allow those for extractvalue and
1487 // insertvalue we need to check array indexing manually.
1488 // Since the only other types we can index into are struct types it's just
1489 // as easy to check those manually as well.
1490 if (const ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
1491 if (Index >= AT->getNumElements())
1492 return 0;
1493 } else if (const StructType *ST = dyn_cast<StructType>(Agg)) {
1494 if (Index >= ST->getNumElements())
1495 return 0;
1496 } else {
1497 // Not a valid type to index into.
1498 return 0;
1499 }
1500
1501 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001502
1503 // If the new type forwards to another type, then it is in the middle
1504 // of being refined to another type (and hence, may have dropped all
1505 // references to what it was using before). So, use the new forwarded
1506 // type.
1507 if (const Type *Ty = Agg->getForwardedType())
1508 Agg = Ty;
1509 }
Frits van Bommel16ebe772010-12-05 20:50:26 +00001510 return Agg;
Dan Gohman12fce772008-05-15 19:50:34 +00001511}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001512
Dan Gohman4a3125b2008-06-17 21:07:55 +00001513const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001514 unsigned Idx) {
1515 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001516}
1517
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001518//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001519// BinaryOperator Class
1520//===----------------------------------------------------------------------===//
1521
Chris Lattner2195fc42007-02-24 00:55:48 +00001522BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001523 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001524 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001525 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001526 OperandTraits<BinaryOperator>::op_begin(this),
1527 OperandTraits<BinaryOperator>::operands(this),
1528 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001529 Op<0>() = S1;
1530 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001531 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001532 setName(Name);
1533}
1534
1535BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001536 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001537 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001538 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001539 OperandTraits<BinaryOperator>::op_begin(this),
1540 OperandTraits<BinaryOperator>::operands(this),
1541 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001542 Op<0>() = S1;
1543 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001544 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001545 setName(Name);
1546}
1547
1548
1549void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001550 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001551 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001552 assert(LHS->getType() == RHS->getType() &&
1553 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001554#ifndef NDEBUG
1555 switch (iType) {
1556 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001557 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001558 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001559 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001560 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001561 "Tried to create an integer operation on a non-integer type!");
1562 break;
1563 case FAdd: case FSub:
1564 case FMul:
1565 assert(getType() == LHS->getType() &&
1566 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001567 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001568 "Tried to create a floating-point operation on a "
1569 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001570 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001571 case UDiv:
1572 case SDiv:
1573 assert(getType() == LHS->getType() &&
1574 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001575 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001576 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001577 "Incorrect operand type (not integer) for S/UDIV");
1578 break;
1579 case FDiv:
1580 assert(getType() == LHS->getType() &&
1581 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001582 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001583 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001584 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001585 case URem:
1586 case SRem:
1587 assert(getType() == LHS->getType() &&
1588 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001589 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001590 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001591 "Incorrect operand type (not integer) for S/UREM");
1592 break;
1593 case FRem:
1594 assert(getType() == LHS->getType() &&
1595 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001596 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001597 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001598 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001599 case Shl:
1600 case LShr:
1601 case AShr:
1602 assert(getType() == LHS->getType() &&
1603 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001604 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001605 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001606 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001607 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001608 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001609 case And: case Or:
1610 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001611 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001612 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001613 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001614 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001615 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001616 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001617 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001618 default:
1619 break;
1620 }
1621#endif
1622}
1623
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001624BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001625 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001626 Instruction *InsertBefore) {
1627 assert(S1->getType() == S2->getType() &&
1628 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001629 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001630}
1631
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001632BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001633 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001634 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001635 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001636 InsertAtEnd->getInstList().push_back(Res);
1637 return Res;
1638}
1639
Dan Gohman5476cfd2009-08-12 16:23:25 +00001640BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001641 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001642 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001643 return new BinaryOperator(Instruction::Sub,
1644 zero, Op,
1645 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001646}
1647
Dan Gohman5476cfd2009-08-12 16:23:25 +00001648BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001649 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001650 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001651 return new BinaryOperator(Instruction::Sub,
1652 zero, Op,
1653 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001654}
1655
Dan Gohman4ab44202009-12-18 02:58:50 +00001656BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1657 Instruction *InsertBefore) {
1658 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1659 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1660}
1661
1662BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1663 BasicBlock *InsertAtEnd) {
1664 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1665 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1666}
1667
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001668BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1669 Instruction *InsertBefore) {
1670 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1671 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1672}
1673
1674BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1675 BasicBlock *InsertAtEnd) {
1676 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1677 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1678}
1679
Dan Gohman5476cfd2009-08-12 16:23:25 +00001680BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001681 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001682 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001683 return new BinaryOperator(Instruction::FSub,
1684 zero, Op,
1685 Op->getType(), Name, InsertBefore);
1686}
1687
Dan Gohman5476cfd2009-08-12 16:23:25 +00001688BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001689 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001690 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001691 return new BinaryOperator(Instruction::FSub,
1692 zero, Op,
1693 Op->getType(), Name, InsertAtEnd);
1694}
1695
Dan Gohman5476cfd2009-08-12 16:23:25 +00001696BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001697 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001698 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001699 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001700 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001701 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001702 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001703 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001704 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001705 }
1706
1707 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001708 Op->getType(), Name, InsertBefore);
1709}
1710
Dan Gohman5476cfd2009-08-12 16:23:25 +00001711BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001712 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001713 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001714 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001715 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001716 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001717 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001718 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001719 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001720 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001721 }
1722
1723 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001724 Op->getType(), Name, InsertAtEnd);
1725}
1726
1727
1728// isConstantAllOnes - Helper function for several functions below
1729static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001730 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1731 return CI->isAllOnesValue();
1732 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1733 return CV->isAllOnesValue();
1734 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001735}
1736
Owen Andersonbb2501b2009-07-13 22:18:28 +00001737bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001738 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1739 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001740 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1741 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001742 return false;
1743}
1744
Owen Andersonbb2501b2009-07-13 22:18:28 +00001745bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001746 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1747 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001748 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001749 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001750 return false;
1751}
1752
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001753bool BinaryOperator::isNot(const Value *V) {
1754 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1755 return (Bop->getOpcode() == Instruction::Xor &&
1756 (isConstantAllOnes(Bop->getOperand(1)) ||
1757 isConstantAllOnes(Bop->getOperand(0))));
1758 return false;
1759}
1760
Chris Lattner2c7d1772005-04-24 07:28:37 +00001761Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001762 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001763}
1764
Chris Lattner2c7d1772005-04-24 07:28:37 +00001765const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1766 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001767}
1768
Dan Gohmana5b96452009-06-04 22:49:04 +00001769Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001770 return cast<BinaryOperator>(BinOp)->getOperand(1);
1771}
1772
1773const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1774 return getFNegArgument(const_cast<Value*>(BinOp));
1775}
1776
Chris Lattner2c7d1772005-04-24 07:28:37 +00001777Value *BinaryOperator::getNotArgument(Value *BinOp) {
1778 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1779 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1780 Value *Op0 = BO->getOperand(0);
1781 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001782 if (isConstantAllOnes(Op0)) return Op1;
1783
1784 assert(isConstantAllOnes(Op1));
1785 return Op0;
1786}
1787
Chris Lattner2c7d1772005-04-24 07:28:37 +00001788const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1789 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001790}
1791
1792
1793// swapOperands - Exchange the two operands to this instruction. This
1794// instruction is safe to use on any binary instruction and does not
1795// modify the semantics of the instruction. If the instruction is
1796// order dependent (SetLT f.e.) the opcode is changed.
1797//
1798bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001799 if (!isCommutative())
1800 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001801 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001802 return false;
1803}
1804
Dan Gohman1b849082009-09-07 23:54:19 +00001805void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1806 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1807}
1808
1809void BinaryOperator::setHasNoSignedWrap(bool b) {
1810 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1811}
1812
1813void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00001814 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00001815}
1816
Nick Lewycky28a5f252009-09-27 21:33:04 +00001817bool BinaryOperator::hasNoUnsignedWrap() const {
1818 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1819}
1820
1821bool BinaryOperator::hasNoSignedWrap() const {
1822 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1823}
1824
1825bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00001826 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00001827}
1828
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001829//===----------------------------------------------------------------------===//
1830// CastInst Class
1831//===----------------------------------------------------------------------===//
1832
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001833// Just determine if this cast only deals with integral->integral conversion.
1834bool CastInst::isIntegerCast() const {
1835 switch (getOpcode()) {
1836 default: return false;
1837 case Instruction::ZExt:
1838 case Instruction::SExt:
1839 case Instruction::Trunc:
1840 return true;
1841 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00001842 return getOperand(0)->getType()->isIntegerTy() &&
1843 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001844 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001845}
1846
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001847bool CastInst::isLosslessCast() const {
1848 // Only BitCast can be lossless, exit fast if we're not BitCast
1849 if (getOpcode() != Instruction::BitCast)
1850 return false;
1851
1852 // Identity cast is always lossless
1853 const Type* SrcTy = getOperand(0)->getType();
1854 const Type* DstTy = getType();
1855 if (SrcTy == DstTy)
1856 return true;
1857
Reid Spencer8d9336d2006-12-31 05:26:44 +00001858 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00001859 if (SrcTy->isPointerTy())
1860 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001861 return false; // Other types have no identity values
1862}
1863
1864/// This function determines if the CastInst does not require any bits to be
1865/// changed in order to effect the cast. Essentially, it identifies cases where
1866/// no code gen is necessary for the cast, hence the name no-op cast. For
1867/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001868/// # bitcast i32* %x to i8*
1869/// # bitcast <2 x i32> %x to <4 x i16>
1870/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001871/// @brief Determine if the described cast is a no-op.
1872bool CastInst::isNoopCast(Instruction::CastOps Opcode,
1873 const Type *SrcTy,
1874 const Type *DestTy,
1875 const Type *IntPtrTy) {
1876 switch (Opcode) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001877 default:
1878 assert(!"Invalid CastOp");
1879 case Instruction::Trunc:
1880 case Instruction::ZExt:
1881 case Instruction::SExt:
1882 case Instruction::FPTrunc:
1883 case Instruction::FPExt:
1884 case Instruction::UIToFP:
1885 case Instruction::SIToFP:
1886 case Instruction::FPToUI:
1887 case Instruction::FPToSI:
1888 return false; // These always modify bits
1889 case Instruction::BitCast:
1890 return true; // BitCast never modifies bits.
1891 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001892 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001893 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001894 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001895 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001896 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001897 }
1898}
1899
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001900/// @brief Determine if a cast is a no-op.
1901bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1902 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
1903}
1904
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001905/// This function determines if a pair of casts can be eliminated and what
1906/// opcode should be used in the elimination. This assumes that there are two
1907/// instructions like this:
1908/// * %F = firstOpcode SrcTy %x to MidTy
1909/// * %S = secondOpcode MidTy %F to DstTy
1910/// The function returns a resultOpcode so these two casts can be replaced with:
1911/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1912/// If no such cast is permited, the function returns 0.
1913unsigned CastInst::isEliminableCastPair(
1914 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1915 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1916{
1917 // Define the 144 possibilities for these two cast instructions. The values
1918 // in this matrix determine what to do in a given situation and select the
1919 // case in the switch below. The rows correspond to firstOp, the columns
1920 // correspond to secondOp. In looking at the table below, keep in mind
1921 // the following cast properties:
1922 //
1923 // Size Compare Source Destination
1924 // Operator Src ? Size Type Sign Type Sign
1925 // -------- ------------ ------------------- ---------------------
1926 // TRUNC > Integer Any Integral Any
1927 // ZEXT < Integral Unsigned Integer Any
1928 // SEXT < Integral Signed Integer Any
1929 // FPTOUI n/a FloatPt n/a Integral Unsigned
1930 // FPTOSI n/a FloatPt n/a Integral Signed
1931 // UITOFP n/a Integral Unsigned FloatPt n/a
1932 // SITOFP n/a Integral Signed FloatPt n/a
1933 // FPTRUNC > FloatPt n/a FloatPt n/a
1934 // FPEXT < FloatPt n/a FloatPt n/a
1935 // PTRTOINT n/a Pointer n/a Integral Unsigned
1936 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00001937 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001938 //
1939 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001940 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1941 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001942 // of the produced value (we no longer know the top-part is all zeros).
1943 // Further this conversion is often much more expensive for typical hardware,
1944 // and causes issues when building libgcc. We disallow fptosi+sext for the
1945 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001946 const unsigned numCastOps =
1947 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1948 static const uint8_t CastResults[numCastOps][numCastOps] = {
1949 // T F F U S F F P I B -+
1950 // R Z S P P I I T P 2 N T |
1951 // U E E 2 2 2 2 R E I T C +- secondOp
1952 // N X X U S F F N X N 2 V |
1953 // C T T I I P P C T T P T -+
1954 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1955 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1956 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001957 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1958 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001959 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1960 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1961 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1962 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1963 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1964 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1965 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1966 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00001967
1968 // If either of the casts are a bitcast from scalar to vector, disallow the
1969 // merging.
1970 if ((firstOp == Instruction::BitCast &&
1971 isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
1972 (secondOp == Instruction::BitCast &&
1973 isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
1974 return 0; // Disallowed
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001975
1976 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1977 [secondOp-Instruction::CastOpsBegin];
1978 switch (ElimCase) {
1979 case 0:
1980 // categorically disallowed
1981 return 0;
1982 case 1:
1983 // allowed, use first cast's opcode
1984 return firstOp;
1985 case 2:
1986 // allowed, use second cast's opcode
1987 return secondOp;
1988 case 3:
1989 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00001990 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00001991 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001992 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001993 return firstOp;
1994 return 0;
1995 case 4:
1996 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00001997 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00001998 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001999 return firstOp;
2000 return 0;
2001 case 5:
2002 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002003 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002004 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002005 return secondOp;
2006 return 0;
2007 case 6:
2008 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002009 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002010 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002011 return secondOp;
2012 return 0;
2013 case 7: {
2014 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002015 if (!IntPtrTy)
2016 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002017 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2018 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002019 if (MidSize >= PtrSize)
2020 return Instruction::BitCast;
2021 return 0;
2022 }
2023 case 8: {
2024 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2025 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2026 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002027 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2028 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002029 if (SrcSize == DstSize)
2030 return Instruction::BitCast;
2031 else if (SrcSize < DstSize)
2032 return firstOp;
2033 return secondOp;
2034 }
2035 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2036 return Instruction::ZExt;
2037 case 10:
2038 // fpext followed by ftrunc is allowed if the bit size returned to is
2039 // the same as the original, in which case its just a bitcast
2040 if (SrcTy == DstTy)
2041 return Instruction::BitCast;
2042 return 0; // If the types are not the same we can't eliminate it.
2043 case 11:
2044 // bitcast followed by ptrtoint is allowed as long as the bitcast
2045 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002046 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002047 return secondOp;
2048 return 0;
2049 case 12:
2050 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002051 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002052 return firstOp;
2053 return 0;
2054 case 13: {
2055 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002056 if (!IntPtrTy)
2057 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002058 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2059 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2060 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002061 if (SrcSize <= PtrSize && SrcSize == DstSize)
2062 return Instruction::BitCast;
2063 return 0;
2064 }
2065 case 99:
2066 // cast combination can't happen (error in input). This is for all cases
2067 // where the MidTy is not the same for the two cast instructions.
2068 assert(!"Invalid Cast Combination");
2069 return 0;
2070 default:
2071 assert(!"Error in CastResults table!!!");
2072 return 0;
2073 }
2074 return 0;
2075}
2076
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002077CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002078 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002079 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002080 // Construct and return the appropriate CastInst subclass
2081 switch (op) {
2082 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2083 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2084 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2085 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2086 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2087 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2088 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2089 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2090 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2091 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2092 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2093 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2094 default:
2095 assert(!"Invalid opcode provided");
2096 }
2097 return 0;
2098}
2099
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002100CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002101 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002102 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002103 // Construct and return the appropriate CastInst subclass
2104 switch (op) {
2105 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2106 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2107 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2108 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2109 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2110 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2111 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2112 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2113 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2114 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2115 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2116 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2117 default:
2118 assert(!"Invalid opcode provided");
2119 }
2120 return 0;
2121}
2122
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002123CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002124 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002125 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002126 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002127 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2128 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002129}
2130
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002131CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002132 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002133 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002134 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002135 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2136 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002137}
2138
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002139CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002140 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002141 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002142 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002143 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2144 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002145}
2146
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002147CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002148 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002149 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002150 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002151 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2152 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002153}
2154
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002155CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002156 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002157 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002158 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002159 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2160 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002161}
2162
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002163CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002164 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002165 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002166 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002167 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2168 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002169}
2170
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002171CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002172 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002173 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002174 assert(S->getType()->isPointerTy() && "Invalid cast");
2175 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002176 "Invalid cast");
2177
Duncan Sands9dff9be2010-02-15 16:12:20 +00002178 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002179 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2180 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002181}
2182
2183/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002184CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002185 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002186 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002187 assert(S->getType()->isPointerTy() && "Invalid cast");
2188 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002189 "Invalid cast");
2190
Duncan Sands9dff9be2010-02-15 16:12:20 +00002191 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002192 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2193 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002194}
2195
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002196CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002197 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002198 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002199 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002200 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002201 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2202 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002203 Instruction::CastOps opcode =
2204 (SrcBits == DstBits ? Instruction::BitCast :
2205 (SrcBits > DstBits ? Instruction::Trunc :
2206 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002207 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002208}
2209
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002210CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002211 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002212 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002213 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002214 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002215 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2216 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002217 Instruction::CastOps opcode =
2218 (SrcBits == DstBits ? Instruction::BitCast :
2219 (SrcBits > DstBits ? Instruction::Trunc :
2220 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002221 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002222}
2223
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002224CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002225 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002226 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002227 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002228 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002229 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2230 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002231 Instruction::CastOps opcode =
2232 (SrcBits == DstBits ? Instruction::BitCast :
2233 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002234 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002235}
2236
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002237CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002238 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002239 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002240 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002241 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002242 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2243 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002244 Instruction::CastOps opcode =
2245 (SrcBits == DstBits ? Instruction::BitCast :
2246 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002247 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002248}
2249
Duncan Sands55e50902008-01-06 10:12:28 +00002250// Check whether it is valid to call getCastOpcode for these types.
2251// This routine must be kept in sync with getCastOpcode.
2252bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2253 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2254 return false;
2255
2256 if (SrcTy == DestTy)
2257 return true;
2258
Duncan Sandsa8514532011-05-18 07:13:41 +00002259 if (const VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2260 if (const VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2261 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2262 // An element by element cast. Valid if casting the elements is valid.
2263 SrcTy = SrcVecTy->getElementType();
2264 DestTy = DestVecTy->getElementType();
2265 }
2266
Duncan Sands55e50902008-01-06 10:12:28 +00002267 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002268 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2269 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002270
2271 // Run through the possibilities ...
Duncan Sands27bd0df2011-05-18 10:59:25 +00002272 if (DestTy->isIntegerTy()) { // Casting to integral
2273 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002274 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002275 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002276 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002277 } else if (SrcTy->isVectorTy()) { // Casting from vector
2278 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002279 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002280 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002281 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002282 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2283 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002284 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002285 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002286 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002287 } else if (SrcTy->isVectorTy()) { // Casting from vector
2288 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002289 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002290 return false;
2291 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002292 } else if (DestTy->isVectorTy()) { // Casting to vector
2293 return DestBits == SrcBits;
Duncan Sands19d0b472010-02-16 11:11:14 +00002294 } else if (DestTy->isPointerTy()) { // Casting to pointer
Duncan Sands27bd0df2011-05-18 10:59:25 +00002295 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002296 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002297 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002298 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002299 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002300 return false;
2301 }
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002302 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002303 if (SrcTy->isVectorTy()) {
2304 return DestBits == SrcBits; // 64-bit vector to MMX
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002305 } else {
2306 return false;
2307 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002308 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002309 return false;
2310 }
2311}
2312
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002313// Provide a way to get a "cast" where the cast opcode is inferred from the
2314// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002315// logic in the castIsValid function below. This axiom should hold:
2316// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2317// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002318// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002319// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002320Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002321CastInst::getCastOpcode(
2322 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002323 const Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002324
Duncan Sands55e50902008-01-06 10:12:28 +00002325 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2326 "Only first class types are castable!");
2327
Duncan Sandsa8514532011-05-18 07:13:41 +00002328 if (SrcTy == DestTy)
2329 return BitCast;
2330
2331 if (const VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2332 if (const VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2333 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2334 // An element by element cast. Find the appropriate opcode based on the
2335 // element types.
2336 SrcTy = SrcVecTy->getElementType();
2337 DestTy = DestVecTy->getElementType();
2338 }
2339
2340 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002341 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2342 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002343
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002344 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002345 if (DestTy->isIntegerTy()) { // Casting to integral
2346 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002347 if (DestBits < SrcBits)
2348 return Trunc; // int -> smaller int
2349 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002350 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002351 return SExt; // signed -> SEXT
2352 else
2353 return ZExt; // unsigned -> ZEXT
2354 } else {
2355 return BitCast; // Same size, No-op cast
2356 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002357 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002358 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002359 return FPToSI; // FP -> sint
2360 else
2361 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002362 } else if (SrcTy->isVectorTy()) {
2363 assert(DestBits == SrcBits &&
2364 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002365 return BitCast; // Same size, no-op cast
2366 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002367 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002368 "Casting from a value that is not first-class type");
2369 return PtrToInt; // ptr -> int
2370 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002371 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2372 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002373 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002374 return SIToFP; // sint -> FP
2375 else
2376 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002377 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002378 if (DestBits < SrcBits) {
2379 return FPTrunc; // FP -> smaller FP
2380 } else if (DestBits > SrcBits) {
2381 return FPExt; // FP -> larger FP
2382 } else {
2383 return BitCast; // same size, no-op cast
2384 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002385 } else if (SrcTy->isVectorTy()) {
2386 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002387 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002388 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002389 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002390 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002391 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002392 } else if (DestTy->isVectorTy()) {
2393 assert(DestBits == SrcBits &&
2394 "Illegal cast to vector (wrong type or size)");
2395 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002396 } else if (DestTy->isPointerTy()) {
2397 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002398 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002399 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002400 return IntToPtr; // int -> ptr
2401 } else {
2402 assert(!"Casting pointer to other than pointer or int");
2403 }
Dale Johannesendd224d22010-09-30 23:57:10 +00002404 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002405 if (SrcTy->isVectorTy()) {
2406 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002407 return BitCast; // 64-bit vector to MMX
2408 } else {
2409 assert(!"Illegal cast to X86_MMX");
2410 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002411 } else {
2412 assert(!"Casting to type that is not first-class");
2413 }
2414
2415 // If we fall through to here we probably hit an assertion cast above
2416 // and assertions are not turned on. Anything we return is an error, so
2417 // BitCast is as good a choice as any.
2418 return BitCast;
2419}
2420
2421//===----------------------------------------------------------------------===//
2422// CastInst SubClass Constructors
2423//===----------------------------------------------------------------------===//
2424
2425/// Check that the construction parameters for a CastInst are correct. This
2426/// could be broken out into the separate constructors but it is useful to have
2427/// it in one place and to eliminate the redundant code for getting the sizes
2428/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002429bool
2430CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002431
2432 // Check for type sanity on the arguments
2433 const Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002434 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2435 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002436 return false;
2437
2438 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002439 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2440 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002441
Duncan Sands7f646562011-05-18 09:21:57 +00002442 // If these are vector types, get the lengths of the vectors (using zero for
2443 // scalar types means that checking that vector lengths match also checks that
2444 // scalars are not being converted to vectors or vectors to scalars).
2445 unsigned SrcLength = SrcTy->isVectorTy() ?
2446 cast<VectorType>(SrcTy)->getNumElements() : 0;
2447 unsigned DstLength = DstTy->isVectorTy() ?
2448 cast<VectorType>(DstTy)->getNumElements() : 0;
2449
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002450 // Switch on the opcode provided
2451 switch (op) {
2452 default: return false; // This is an input error
2453 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002454 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2455 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002456 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002457 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2458 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002459 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002460 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2461 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002462 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002463 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2464 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002465 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002466 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2467 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002468 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002469 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002470 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2471 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002472 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002473 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002474 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2475 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002476 case Instruction::PtrToInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002477 return SrcTy->isPointerTy() && DstTy->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002478 case Instruction::IntToPtr:
Duncan Sands19d0b472010-02-16 11:11:14 +00002479 return SrcTy->isIntegerTy() && DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002480 case Instruction::BitCast:
2481 // BitCast implies a no-op cast of type only. No bits change.
2482 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002483 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002484 return false;
2485
Duncan Sands55e50902008-01-06 10:12:28 +00002486 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002487 // these cases, the cast is okay if the source and destination bit widths
2488 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002489 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002490 }
2491}
2492
2493TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002494 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002495) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002496 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002497}
2498
2499TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002500 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002501) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002502 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002503}
2504
2505ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002506 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002507) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002508 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002509}
2510
2511ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002512 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002513) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002514 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002515}
2516SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002517 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002518) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002519 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002520}
2521
Jeff Cohencc08c832006-12-02 02:22:01 +00002522SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002523 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002524) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002525 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002526}
2527
2528FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002529 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002530) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002531 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002532}
2533
2534FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002535 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002536) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002537 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002538}
2539
2540FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002541 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002542) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002543 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002544}
2545
2546FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002547 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002548) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002549 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002550}
2551
2552UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002553 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002554) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002555 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002556}
2557
2558UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002559 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002560) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002561 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002562}
2563
2564SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002565 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002566) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002567 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002568}
2569
2570SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002571 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002572) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002573 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002574}
2575
2576FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002577 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002578) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002579 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002580}
2581
2582FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002583 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002584) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002585 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002586}
2587
2588FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002589 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002590) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002591 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002592}
2593
2594FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002595 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002596) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002597 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002598}
2599
2600PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002601 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002602) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002603 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002604}
2605
2606PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002607 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002608) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002609 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002610}
2611
2612IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002613 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002614) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002615 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002616}
2617
2618IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002619 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002620) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002621 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002622}
2623
2624BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002625 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002626) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002627 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002628}
2629
2630BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002631 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002632) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002633 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002634}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002635
2636//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002637// CmpInst Classes
2638//===----------------------------------------------------------------------===//
2639
Chris Lattneraec33da2010-01-22 06:25:37 +00002640void CmpInst::Anchor() const {}
2641
Nate Begemand2195702008-05-12 19:01:56 +00002642CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002643 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002644 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002645 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002646 OperandTraits<CmpInst>::op_begin(this),
2647 OperandTraits<CmpInst>::operands(this),
2648 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002649 Op<0>() = LHS;
2650 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002651 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002652 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002653}
Gabor Greiff6caff662008-05-10 08:32:32 +00002654
Nate Begemand2195702008-05-12 19:01:56 +00002655CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002656 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002657 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002658 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002659 OperandTraits<CmpInst>::op_begin(this),
2660 OperandTraits<CmpInst>::operands(this),
2661 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002662 Op<0>() = LHS;
2663 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002664 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002665 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002666}
2667
2668CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002669CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002670 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002671 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002672 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002673 if (InsertBefore)
2674 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2675 S1, S2, Name);
2676 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002677 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002678 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002679 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002680
2681 if (InsertBefore)
2682 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2683 S1, S2, Name);
2684 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002685 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002686 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002687}
2688
2689CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002690CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002691 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002692 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002693 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2694 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002695 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002696 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2697 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002698}
2699
2700void CmpInst::swapOperands() {
2701 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2702 IC->swapOperands();
2703 else
2704 cast<FCmpInst>(this)->swapOperands();
2705}
2706
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002707bool CmpInst::isCommutative() const {
2708 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002709 return IC->isCommutative();
2710 return cast<FCmpInst>(this)->isCommutative();
2711}
2712
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002713bool CmpInst::isEquality() const {
2714 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002715 return IC->isEquality();
2716 return cast<FCmpInst>(this)->isEquality();
2717}
2718
2719
Dan Gohman4e724382008-05-31 02:47:54 +00002720CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002721 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002722 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002723 case ICMP_EQ: return ICMP_NE;
2724 case ICMP_NE: return ICMP_EQ;
2725 case ICMP_UGT: return ICMP_ULE;
2726 case ICMP_ULT: return ICMP_UGE;
2727 case ICMP_UGE: return ICMP_ULT;
2728 case ICMP_ULE: return ICMP_UGT;
2729 case ICMP_SGT: return ICMP_SLE;
2730 case ICMP_SLT: return ICMP_SGE;
2731 case ICMP_SGE: return ICMP_SLT;
2732 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002733
Dan Gohman4e724382008-05-31 02:47:54 +00002734 case FCMP_OEQ: return FCMP_UNE;
2735 case FCMP_ONE: return FCMP_UEQ;
2736 case FCMP_OGT: return FCMP_ULE;
2737 case FCMP_OLT: return FCMP_UGE;
2738 case FCMP_OGE: return FCMP_ULT;
2739 case FCMP_OLE: return FCMP_UGT;
2740 case FCMP_UEQ: return FCMP_ONE;
2741 case FCMP_UNE: return FCMP_OEQ;
2742 case FCMP_UGT: return FCMP_OLE;
2743 case FCMP_ULT: return FCMP_OGE;
2744 case FCMP_UGE: return FCMP_OLT;
2745 case FCMP_ULE: return FCMP_OGT;
2746 case FCMP_ORD: return FCMP_UNO;
2747 case FCMP_UNO: return FCMP_ORD;
2748 case FCMP_TRUE: return FCMP_FALSE;
2749 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002750 }
2751}
2752
Reid Spencer266e42b2006-12-23 06:05:41 +00002753ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2754 switch (pred) {
2755 default: assert(! "Unknown icmp predicate!");
2756 case ICMP_EQ: case ICMP_NE:
2757 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2758 return pred;
2759 case ICMP_UGT: return ICMP_SGT;
2760 case ICMP_ULT: return ICMP_SLT;
2761 case ICMP_UGE: return ICMP_SGE;
2762 case ICMP_ULE: return ICMP_SLE;
2763 }
2764}
2765
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002766ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2767 switch (pred) {
2768 default: assert(! "Unknown icmp predicate!");
2769 case ICMP_EQ: case ICMP_NE:
2770 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2771 return pred;
2772 case ICMP_SGT: return ICMP_UGT;
2773 case ICMP_SLT: return ICMP_ULT;
2774 case ICMP_SGE: return ICMP_UGE;
2775 case ICMP_SLE: return ICMP_ULE;
2776 }
2777}
2778
Reid Spencer0286bc12007-02-28 22:00:54 +00002779/// Initialize a set of values that all satisfy the condition with C.
2780///
2781ConstantRange
2782ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2783 APInt Lower(C);
2784 APInt Upper(C);
2785 uint32_t BitWidth = C.getBitWidth();
2786 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002787 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002788 case ICmpInst::ICMP_EQ: Upper++; break;
2789 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00002790 case ICmpInst::ICMP_ULT:
2791 Lower = APInt::getMinValue(BitWidth);
2792 // Check for an empty-set condition.
2793 if (Lower == Upper)
2794 return ConstantRange(BitWidth, /*isFullSet=*/false);
2795 break;
2796 case ICmpInst::ICMP_SLT:
2797 Lower = APInt::getSignedMinValue(BitWidth);
2798 // Check for an empty-set condition.
2799 if (Lower == Upper)
2800 return ConstantRange(BitWidth, /*isFullSet=*/false);
2801 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00002802 case ICmpInst::ICMP_UGT:
2803 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002804 // Check for an empty-set condition.
2805 if (Lower == Upper)
2806 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002807 break;
2808 case ICmpInst::ICMP_SGT:
2809 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002810 // Check for an empty-set condition.
2811 if (Lower == Upper)
2812 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002813 break;
2814 case ICmpInst::ICMP_ULE:
2815 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002816 // Check for a full-set condition.
2817 if (Lower == Upper)
2818 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002819 break;
2820 case ICmpInst::ICMP_SLE:
2821 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002822 // Check for a full-set condition.
2823 if (Lower == Upper)
2824 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002825 break;
2826 case ICmpInst::ICMP_UGE:
2827 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002828 // Check for a full-set condition.
2829 if (Lower == Upper)
2830 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002831 break;
2832 case ICmpInst::ICMP_SGE:
2833 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002834 // Check for a full-set condition.
2835 if (Lower == Upper)
2836 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002837 break;
2838 }
2839 return ConstantRange(Lower, Upper);
2840}
2841
Dan Gohman4e724382008-05-31 02:47:54 +00002842CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002843 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002844 default: assert(!"Unknown cmp predicate!");
2845 case ICMP_EQ: case ICMP_NE:
2846 return pred;
2847 case ICMP_SGT: return ICMP_SLT;
2848 case ICMP_SLT: return ICMP_SGT;
2849 case ICMP_SGE: return ICMP_SLE;
2850 case ICMP_SLE: return ICMP_SGE;
2851 case ICMP_UGT: return ICMP_ULT;
2852 case ICMP_ULT: return ICMP_UGT;
2853 case ICMP_UGE: return ICMP_ULE;
2854 case ICMP_ULE: return ICMP_UGE;
2855
Reid Spencerd9436b62006-11-20 01:22:35 +00002856 case FCMP_FALSE: case FCMP_TRUE:
2857 case FCMP_OEQ: case FCMP_ONE:
2858 case FCMP_UEQ: case FCMP_UNE:
2859 case FCMP_ORD: case FCMP_UNO:
2860 return pred;
2861 case FCMP_OGT: return FCMP_OLT;
2862 case FCMP_OLT: return FCMP_OGT;
2863 case FCMP_OGE: return FCMP_OLE;
2864 case FCMP_OLE: return FCMP_OGE;
2865 case FCMP_UGT: return FCMP_ULT;
2866 case FCMP_ULT: return FCMP_UGT;
2867 case FCMP_UGE: return FCMP_ULE;
2868 case FCMP_ULE: return FCMP_UGE;
2869 }
2870}
2871
Reid Spencer266e42b2006-12-23 06:05:41 +00002872bool CmpInst::isUnsigned(unsigned short predicate) {
2873 switch (predicate) {
2874 default: return false;
2875 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2876 case ICmpInst::ICMP_UGE: return true;
2877 }
2878}
2879
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002880bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002881 switch (predicate) {
2882 default: return false;
2883 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2884 case ICmpInst::ICMP_SGE: return true;
2885 }
2886}
2887
2888bool CmpInst::isOrdered(unsigned short predicate) {
2889 switch (predicate) {
2890 default: return false;
2891 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2892 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2893 case FCmpInst::FCMP_ORD: return true;
2894 }
2895}
2896
2897bool CmpInst::isUnordered(unsigned short predicate) {
2898 switch (predicate) {
2899 default: return false;
2900 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2901 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2902 case FCmpInst::FCMP_UNO: return true;
2903 }
2904}
2905
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002906bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2907 switch(predicate) {
2908 default: return false;
2909 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2910 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2911 }
2912}
2913
2914bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2915 switch(predicate) {
2916 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2917 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2918 default: return false;
2919 }
2920}
2921
2922
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002923//===----------------------------------------------------------------------===//
2924// SwitchInst Implementation
2925//===----------------------------------------------------------------------===//
2926
Chris Lattnerbaf00152010-11-17 05:41:46 +00002927void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
2928 assert(Value && Default && NumReserved);
2929 ReservedSpace = NumReserved;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002930 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002931 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002932
Gabor Greif2d3024d2008-05-26 21:33:52 +00002933 OperandList[0] = Value;
2934 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002935}
2936
Chris Lattner2195fc42007-02-24 00:55:48 +00002937/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2938/// switch on and a default destination. The number of additional cases can
2939/// be specified here to make memory allocation more efficient. This
2940/// constructor can also autoinsert before another instruction.
2941SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2942 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002943 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2944 0, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00002945 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00002946}
2947
2948/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2949/// switch on and a default destination. The number of additional cases can
2950/// be specified here to make memory allocation more efficient. This
2951/// constructor also autoinserts at the end of the specified BasicBlock.
2952SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2953 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002954 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2955 0, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00002956 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00002957}
2958
Misha Brukmanb1c93172005-04-21 23:48:37 +00002959SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerbaf00152010-11-17 05:41:46 +00002960 : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) {
2961 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
2962 NumOperands = SI.getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002963 Use *OL = OperandList, *InOL = SI.OperandList;
Chris Lattnerbaf00152010-11-17 05:41:46 +00002964 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002965 OL[i] = InOL[i];
2966 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002967 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002968 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002969}
2970
Gordon Henriksen14a55692007-12-10 02:14:30 +00002971SwitchInst::~SwitchInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00002972 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002973}
2974
2975
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002976/// addCase - Add an entry to the switch instruction...
2977///
Chris Lattner47ac1872005-02-24 05:32:09 +00002978void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002979 unsigned OpNo = NumOperands;
2980 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00002981 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002982 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002983 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002984 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002985 OperandList[OpNo] = OnVal;
2986 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002987}
2988
2989/// removeCase - This method removes the specified successor from the switch
2990/// instruction. Note that this cannot be used to remove the default
2991/// destination (successor #0).
2992///
2993void SwitchInst::removeCase(unsigned idx) {
2994 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002995 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2996
2997 unsigned NumOps = getNumOperands();
2998 Use *OL = OperandList;
2999
Jay Foad14277722011-02-01 09:22:34 +00003000 // Overwrite this case with the end of the list.
3001 if ((idx + 1) * 2 != NumOps) {
3002 OL[idx * 2] = OL[NumOps - 2];
3003 OL[idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003004 }
3005
3006 // Nuke the last value.
3007 OL[NumOps-2].set(0);
3008 OL[NumOps-2+1].set(0);
3009 NumOperands = NumOps-2;
3010}
3011
Jay Foade98f29d2011-04-01 08:00:58 +00003012/// growOperands - grow operands - This grows the operand list in response
3013/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003014///
Jay Foade98f29d2011-04-01 08:00:58 +00003015void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003016 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003017 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003018
3019 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003020 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003021 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003022 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003023 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003024 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003025 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003026 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003027}
3028
3029
3030BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3031 return getSuccessor(idx);
3032}
3033unsigned SwitchInst::getNumSuccessorsV() const {
3034 return getNumSuccessors();
3035}
3036void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3037 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003038}
Chris Lattnerf22be932004-10-15 23:52:53 +00003039
Chris Lattner3ed871f2009-10-27 19:13:16 +00003040//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003041// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003042//===----------------------------------------------------------------------===//
3043
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003044void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003045 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003046 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003047 ReservedSpace = 1+NumDests;
3048 NumOperands = 1;
3049 OperandList = allocHungoffUses(ReservedSpace);
3050
3051 OperandList[0] = Address;
3052}
3053
3054
Jay Foade98f29d2011-04-01 08:00:58 +00003055/// growOperands - grow operands - This grows the operand list in response
3056/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003057///
Jay Foade98f29d2011-04-01 08:00:58 +00003058void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003059 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003060 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003061
3062 ReservedSpace = NumOps;
3063 Use *NewOps = allocHungoffUses(NumOps);
3064 Use *OldOps = OperandList;
3065 for (unsigned i = 0; i != e; ++i)
3066 NewOps[i] = OldOps[i];
3067 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003068 Use::zap(OldOps, OldOps + e, true);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003069}
3070
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003071IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3072 Instruction *InsertBefore)
3073: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003074 0, 0, InsertBefore) {
3075 init(Address, NumCases);
3076}
3077
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003078IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3079 BasicBlock *InsertAtEnd)
3080: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003081 0, 0, InsertAtEnd) {
3082 init(Address, NumCases);
3083}
3084
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003085IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3086 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003087 allocHungoffUses(IBI.getNumOperands()),
3088 IBI.getNumOperands()) {
3089 Use *OL = OperandList, *InOL = IBI.OperandList;
3090 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3091 OL[i] = InOL[i];
3092 SubclassOptionalData = IBI.SubclassOptionalData;
3093}
3094
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003095IndirectBrInst::~IndirectBrInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003096 dropHungoffUses();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003097}
3098
3099/// addDestination - Add a destination.
3100///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003101void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003102 unsigned OpNo = NumOperands;
3103 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003104 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003105 // Initialize some new operands.
3106 assert(OpNo < ReservedSpace && "Growing didn't work!");
3107 NumOperands = OpNo+1;
3108 OperandList[OpNo] = DestBB;
3109}
3110
3111/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003112/// indirectbr instruction.
3113void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003114 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3115
3116 unsigned NumOps = getNumOperands();
3117 Use *OL = OperandList;
3118
3119 // Replace this value with the last one.
3120 OL[idx+1] = OL[NumOps-1];
3121
3122 // Nuke the last value.
3123 OL[NumOps-1].set(0);
3124 NumOperands = NumOps-1;
3125}
3126
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003127BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003128 return getSuccessor(idx);
3129}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003130unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003131 return getNumSuccessors();
3132}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003133void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003134 setSuccessor(idx, B);
3135}
3136
3137//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003138// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003139//===----------------------------------------------------------------------===//
3140
Chris Lattnerf22be932004-10-15 23:52:53 +00003141// Define these methods here so vtables don't get emitted into every translation
3142// unit that uses these classes.
3143
Devang Patel11cf3f42009-10-27 22:16:29 +00003144GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3145 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003146}
3147
Devang Patel11cf3f42009-10-27 22:16:29 +00003148BinaryOperator *BinaryOperator::clone_impl() const {
3149 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003150}
3151
Devang Patel11cf3f42009-10-27 22:16:29 +00003152FCmpInst* FCmpInst::clone_impl() const {
3153 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003154}
3155
Devang Patel11cf3f42009-10-27 22:16:29 +00003156ICmpInst* ICmpInst::clone_impl() const {
3157 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003158}
3159
Devang Patel11cf3f42009-10-27 22:16:29 +00003160ExtractValueInst *ExtractValueInst::clone_impl() const {
3161 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003162}
3163
Devang Patel11cf3f42009-10-27 22:16:29 +00003164InsertValueInst *InsertValueInst::clone_impl() const {
3165 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003166}
3167
Devang Patel11cf3f42009-10-27 22:16:29 +00003168AllocaInst *AllocaInst::clone_impl() const {
3169 return new AllocaInst(getAllocatedType(),
3170 (Value*)getOperand(0),
3171 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003172}
3173
Devang Patel11cf3f42009-10-27 22:16:29 +00003174LoadInst *LoadInst::clone_impl() const {
3175 return new LoadInst(getOperand(0),
3176 Twine(), isVolatile(),
3177 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003178}
3179
Devang Patel11cf3f42009-10-27 22:16:29 +00003180StoreInst *StoreInst::clone_impl() const {
3181 return new StoreInst(getOperand(0), getOperand(1),
3182 isVolatile(), getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003183}
3184
Devang Patel11cf3f42009-10-27 22:16:29 +00003185TruncInst *TruncInst::clone_impl() const {
3186 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003187}
3188
Devang Patel11cf3f42009-10-27 22:16:29 +00003189ZExtInst *ZExtInst::clone_impl() const {
3190 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003191}
3192
Devang Patel11cf3f42009-10-27 22:16:29 +00003193SExtInst *SExtInst::clone_impl() const {
3194 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003195}
3196
Devang Patel11cf3f42009-10-27 22:16:29 +00003197FPTruncInst *FPTruncInst::clone_impl() const {
3198 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003199}
3200
Devang Patel11cf3f42009-10-27 22:16:29 +00003201FPExtInst *FPExtInst::clone_impl() const {
3202 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003203}
3204
Devang Patel11cf3f42009-10-27 22:16:29 +00003205UIToFPInst *UIToFPInst::clone_impl() const {
3206 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003207}
3208
Devang Patel11cf3f42009-10-27 22:16:29 +00003209SIToFPInst *SIToFPInst::clone_impl() const {
3210 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003211}
3212
Devang Patel11cf3f42009-10-27 22:16:29 +00003213FPToUIInst *FPToUIInst::clone_impl() const {
3214 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003215}
3216
Devang Patel11cf3f42009-10-27 22:16:29 +00003217FPToSIInst *FPToSIInst::clone_impl() const {
3218 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003219}
3220
Devang Patel11cf3f42009-10-27 22:16:29 +00003221PtrToIntInst *PtrToIntInst::clone_impl() const {
3222 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003223}
3224
Devang Patel11cf3f42009-10-27 22:16:29 +00003225IntToPtrInst *IntToPtrInst::clone_impl() const {
3226 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003227}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003228
Devang Patel11cf3f42009-10-27 22:16:29 +00003229BitCastInst *BitCastInst::clone_impl() const {
3230 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003231}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003232
Devang Patel11cf3f42009-10-27 22:16:29 +00003233CallInst *CallInst::clone_impl() const {
3234 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003235}
3236
Devang Patel11cf3f42009-10-27 22:16:29 +00003237SelectInst *SelectInst::clone_impl() const {
3238 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003239}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003240
Devang Patel11cf3f42009-10-27 22:16:29 +00003241VAArgInst *VAArgInst::clone_impl() const {
3242 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003243}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003244
Devang Patel11cf3f42009-10-27 22:16:29 +00003245ExtractElementInst *ExtractElementInst::clone_impl() const {
3246 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003247}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003248
Devang Patel11cf3f42009-10-27 22:16:29 +00003249InsertElementInst *InsertElementInst::clone_impl() const {
3250 return InsertElementInst::Create(getOperand(0),
3251 getOperand(1),
3252 getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003253}
3254
Devang Patel11cf3f42009-10-27 22:16:29 +00003255ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3256 return new ShuffleVectorInst(getOperand(0),
3257 getOperand(1),
3258 getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003259}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003260
Devang Patel11cf3f42009-10-27 22:16:29 +00003261PHINode *PHINode::clone_impl() const {
3262 return new PHINode(*this);
3263}
3264
3265ReturnInst *ReturnInst::clone_impl() const {
3266 return new(getNumOperands()) ReturnInst(*this);
3267}
3268
3269BranchInst *BranchInst::clone_impl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003270 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003271}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003272
Devang Patel11cf3f42009-10-27 22:16:29 +00003273SwitchInst *SwitchInst::clone_impl() const {
3274 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003275}
3276
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003277IndirectBrInst *IndirectBrInst::clone_impl() const {
3278 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003279}
3280
3281
Devang Patel11cf3f42009-10-27 22:16:29 +00003282InvokeInst *InvokeInst::clone_impl() const {
3283 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003284}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003285
Devang Patel11cf3f42009-10-27 22:16:29 +00003286UnwindInst *UnwindInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003287 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003288 return new UnwindInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003289}
3290
Devang Patel11cf3f42009-10-27 22:16:29 +00003291UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003292 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003293 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003294}