blob: 8e484910d2d7597e588531f150addcd427f35084 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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
15#include "llvm/BasicBlock.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Reid Spencerce38beb2007-04-09 18:00:57 +000020#include "llvm/ParameterAttributes.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000021#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000022#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000023#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000024using namespace llvm;
25
Chris Lattnerf7b6d312005-05-06 20:26:43 +000026unsigned CallSite::getCallingConv() const {
27 if (CallInst *CI = dyn_cast<CallInst>(I))
28 return CI->getCallingConv();
29 else
30 return cast<InvokeInst>(I)->getCallingConv();
31}
32void CallSite::setCallingConv(unsigned CC) {
33 if (CallInst *CI = dyn_cast<CallInst>(I))
34 CI->setCallingConv(CC);
35 else
36 cast<InvokeInst>(I)->setCallingConv(CC);
37}
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000038const ParamAttrsList* CallSite::getParamAttrs() const {
39 if (CallInst *CI = dyn_cast<CallInst>(I))
40 return CI->getParamAttrs();
41 else
42 return cast<InvokeInst>(I)->getParamAttrs();
43}
44void CallSite::setParamAttrs(const ParamAttrsList *PAL) {
45 if (CallInst *CI = dyn_cast<CallInst>(I))
46 CI->setParamAttrs(PAL);
47 else
48 cast<InvokeInst>(I)->setParamAttrs(PAL);
49}
Duncan Sands5208d1a2007-11-28 17:07:01 +000050bool CallSite::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
51 if (CallInst *CI = dyn_cast<CallInst>(I))
52 return CI->paramHasAttr(i, attr);
53 else
54 return cast<InvokeInst>(I)->paramHasAttr(i, attr);
55}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000056
57
Chris Lattner1c12a882006-06-21 16:53:47 +000058
59
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000060//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000061// TerminatorInst Class
62//===----------------------------------------------------------------------===//
63
Chris Lattner1c12a882006-06-21 16:53:47 +000064// Out of line virtual method, so the vtable, etc has a home.
65TerminatorInst::~TerminatorInst() {
66}
67
68// Out of line virtual method, so the vtable, etc has a home.
69UnaryInstruction::~UnaryInstruction() {
70}
Chris Lattnerafdb3de2005-01-29 00:35:16 +000071
72
73//===----------------------------------------------------------------------===//
74// PHINode Class
75//===----------------------------------------------------------------------===//
76
77PHINode::PHINode(const PHINode &PN)
78 : Instruction(PN.getType(), Instruction::PHI,
79 new Use[PN.getNumOperands()], PN.getNumOperands()),
80 ReservedSpace(PN.getNumOperands()) {
81 Use *OL = OperandList;
82 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
83 OL[i].init(PN.getOperand(i), this);
84 OL[i+1].init(PN.getOperand(i+1), this);
85 }
86}
87
88PHINode::~PHINode() {
89 delete [] OperandList;
90}
91
92// removeIncomingValue - Remove an incoming value. This is useful if a
93// predecessor basic block is deleted.
94Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
95 unsigned NumOps = getNumOperands();
96 Use *OL = OperandList;
97 assert(Idx*2 < NumOps && "BB not in PHI node!");
98 Value *Removed = OL[Idx*2];
99
100 // Move everything after this operand down.
101 //
102 // FIXME: we could just swap with the end of the list, then erase. However,
103 // client might not expect this to happen. The code as it is thrashes the
104 // use/def lists, which is kinda lame.
105 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
106 OL[i-2] = OL[i];
107 OL[i-2+1] = OL[i+1];
108 }
109
110 // Nuke the last value.
111 OL[NumOps-2].set(0);
112 OL[NumOps-2+1].set(0);
113 NumOperands = NumOps-2;
114
115 // If the PHI node is dead, because it has zero entries, nuke it now.
116 if (NumOps == 2 && DeletePHIIfEmpty) {
117 // If anyone is using this PHI, make them use a dummy value instead...
118 replaceAllUsesWith(UndefValue::get(getType()));
119 eraseFromParent();
120 }
121 return Removed;
122}
123
124/// resizeOperands - resize operands - This adjusts the length of the operands
125/// list according to the following behavior:
126/// 1. If NumOps == 0, grow the operand list in response to a push_back style
127/// of operation. This grows the number of ops by 1.5 times.
128/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
129/// 3. If NumOps == NumOperands, trim the reserved space.
130///
131void PHINode::resizeOperands(unsigned NumOps) {
132 if (NumOps == 0) {
133 NumOps = (getNumOperands())*3/2;
134 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
135 } else if (NumOps*2 > NumOperands) {
136 // No resize needed.
137 if (ReservedSpace >= NumOps) return;
138 } else if (NumOps == NumOperands) {
139 if (ReservedSpace == NumOps) return;
140 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000141 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000142 }
143
144 ReservedSpace = NumOps;
145 Use *NewOps = new Use[NumOps];
146 Use *OldOps = OperandList;
147 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
148 NewOps[i].init(OldOps[i], this);
149 OldOps[i].set(0);
150 }
151 delete [] OldOps;
152 OperandList = NewOps;
153}
154
Nate Begemanb3923212005-08-04 23:24:19 +0000155/// hasConstantValue - If the specified PHI node always merges together the same
156/// value, return the value, otherwise return null.
157///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000158Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000159 // If the PHI node only has one incoming value, eliminate the PHI node...
160 if (getNumIncomingValues() == 1)
Chris Lattner6e709c12005-08-05 15:37:31 +0000161 if (getIncomingValue(0) != this) // not X = phi X
162 return getIncomingValue(0);
163 else
164 return UndefValue::get(getType()); // Self cycle is dead.
165
Nate Begemanb3923212005-08-04 23:24:19 +0000166 // Otherwise if all of the incoming values are the same for the PHI, replace
167 // the PHI node with the incoming value.
168 //
169 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000170 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000171 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000172 if (isa<UndefValue>(getIncomingValue(i)))
173 HasUndefInput = true;
174 else if (getIncomingValue(i) != this) // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000175 if (InVal && getIncomingValue(i) != InVal)
176 return 0; // Not the same, bail out.
177 else
178 InVal = getIncomingValue(i);
179
180 // The only case that could cause InVal to be null is if we have a PHI node
181 // that only has entries for itself. In this case, there is no entry into the
182 // loop, so kill the PHI.
183 //
184 if (InVal == 0) InVal = UndefValue::get(getType());
185
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000186 // If we have a PHI node like phi(X, undef, X), where X is defined by some
187 // instruction, we cannot always return X as the result of the PHI node. Only
188 // do this if X is not an instruction (thus it must dominate the PHI block),
189 // or if the client is prepared to deal with this possibility.
190 if (HasUndefInput && !AllowNonDominatingInstruction)
191 if (Instruction *IV = dyn_cast<Instruction>(InVal))
192 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000193 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000194 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000195 return 0; // Cannot guarantee that InVal dominates this PHINode.
196
Nate Begemanb3923212005-08-04 23:24:19 +0000197 // All of the incoming values are the same, return the value now.
198 return InVal;
199}
200
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000201
202//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000203// CallInst Implementation
204//===----------------------------------------------------------------------===//
205
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000206CallInst::~CallInst() {
207 delete [] OperandList;
Reid Spencerc6a83842007-04-22 17:28:03 +0000208 if (ParamAttrs)
209 ParamAttrs->dropRef();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000210}
211
Chris Lattner054ba2c2007-02-13 00:58:44 +0000212void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Reid Spencer019c8862007-04-09 15:01:12 +0000213 ParamAttrs = 0;
Chris Lattner054ba2c2007-02-13 00:58:44 +0000214 NumOperands = NumParams+1;
215 Use *OL = OperandList = new Use[NumParams+1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000216 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000217
Misha Brukmanb1c93172005-04-21 23:48:37 +0000218 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000219 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000220 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000221
Chris Lattner054ba2c2007-02-13 00:58:44 +0000222 assert((NumParams == FTy->getNumParams() ||
223 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000224 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000225 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000226 assert((i >= FTy->getNumParams() ||
227 FTy->getParamType(i) == Params[i]->getType()) &&
228 "Calling a function with a bad signature!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000229 OL[i+1].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000230 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000231}
232
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000233void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Reid Spencer019c8862007-04-09 15:01:12 +0000234 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000235 NumOperands = 3;
236 Use *OL = OperandList = new Use[3];
237 OL[0].init(Func, this);
238 OL[1].init(Actual1, this);
239 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000240
241 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000242 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000243 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000244
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000245 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000246 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000247 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000248 assert((0 >= FTy->getNumParams() ||
249 FTy->getParamType(0) == Actual1->getType()) &&
250 "Calling a function with a bad signature!");
251 assert((1 >= FTy->getNumParams() ||
252 FTy->getParamType(1) == Actual2->getType()) &&
253 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000254}
255
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000256void CallInst::init(Value *Func, Value *Actual) {
Reid Spencer019c8862007-04-09 15:01:12 +0000257 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000258 NumOperands = 2;
259 Use *OL = OperandList = new Use[2];
260 OL[0].init(Func, this);
261 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000262
263 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000264 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000265 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000266
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000267 assert((FTy->getNumParams() == 1 ||
268 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000269 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000270 assert((0 == FTy->getNumParams() ||
271 FTy->getParamType(0) == Actual->getType()) &&
272 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000273}
274
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000275void CallInst::init(Value *Func) {
Reid Spencer019c8862007-04-09 15:01:12 +0000276 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000277 NumOperands = 1;
278 Use *OL = OperandList = new Use[1];
279 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000280
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000281 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000282 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000283 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000284
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000285 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000286}
287
David Greene17a5dfe2007-08-01 03:43:44 +0000288#if 0
289// Leave for llvm-gcc
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000290CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000291 const std::string &Name, BasicBlock *InsertAtEnd)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000292 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
David Greene17a5dfe2007-08-01 03:43:44 +0000293 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000294 Instruction::Call, 0, 0, InsertAtEnd) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000295 init(Func, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000296 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000297}
298CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
299 const std::string &Name, Instruction *InsertBefore)
David Greene17a5dfe2007-08-01 03:43:44 +0000300 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
301 ->getElementType())->getReturnType(),
302 Instruction::Call, 0, 0, InsertBefore) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000303 init(Func, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000304 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000305}
306
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000307CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
308 const std::string &Name, Instruction *InsertBefore)
309 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
310 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000311 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000312 init(Func, Actual1, Actual2);
Chris Lattner2195fc42007-02-24 00:55:48 +0000313 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000314}
315
316CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
317 const std::string &Name, BasicBlock *InsertAtEnd)
318 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
319 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000320 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000321 init(Func, Actual1, Actual2);
Chris Lattner2195fc42007-02-24 00:55:48 +0000322 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000323}
David Greene17a5dfe2007-08-01 03:43:44 +0000324#endif
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000325CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000326 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000327 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
328 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000329 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000330 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000331 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000332}
333
334CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
335 BasicBlock *InsertAtEnd)
336 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
337 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000338 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000339 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000340 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000341}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000342CallInst::CallInst(Value *Func, const std::string &Name,
343 Instruction *InsertBefore)
344 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
345 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000346 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000347 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000348 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000349}
350
351CallInst::CallInst(Value *Func, const std::string &Name,
352 BasicBlock *InsertAtEnd)
353 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
354 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000355 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000356 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000357 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000358}
359
Misha Brukmanb1c93172005-04-21 23:48:37 +0000360CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000361 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000362 CI.getNumOperands()),
363 ParamAttrs(0) {
364 setParamAttrs(CI.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000365 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000366 Use *OL = OperandList;
367 Use *InOL = CI.OperandList;
368 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
369 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000370}
371
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000372void CallInst::setParamAttrs(const ParamAttrsList *newAttrs) {
373 if (ParamAttrs == newAttrs)
374 return;
375
Reid Spencerc6a83842007-04-22 17:28:03 +0000376 if (ParamAttrs)
377 ParamAttrs->dropRef();
378
379 if (newAttrs)
380 newAttrs->addRef();
381
382 ParamAttrs = newAttrs;
383}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000384
Duncan Sands5208d1a2007-11-28 17:07:01 +0000385bool CallInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
386 if (ParamAttrs && ParamAttrs->paramHasAttr(i, attr))
387 return true;
388 const Function *F = getCalledFunction();
389 return F && F->getParamAttrs() && F->getParamAttrs()->paramHasAttr(i, attr);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000390}
391
Duncan Sands5208d1a2007-11-28 17:07:01 +0000392
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000393//===----------------------------------------------------------------------===//
394// InvokeInst Implementation
395//===----------------------------------------------------------------------===//
396
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000397InvokeInst::~InvokeInst() {
398 delete [] OperandList;
Reid Spencerc6a83842007-04-22 17:28:03 +0000399 if (ParamAttrs)
400 ParamAttrs->dropRef();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000401}
402
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000403void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000404 Value* const *Args, unsigned NumArgs) {
Reid Spencerce38beb2007-04-09 18:00:57 +0000405 ParamAttrs = 0;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000406 NumOperands = 3+NumArgs;
407 Use *OL = OperandList = new Use[3+NumArgs];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000408 OL[0].init(Fn, this);
409 OL[1].init(IfNormal, this);
410 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000411 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000412 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000413 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000414
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000415 assert((NumArgs == FTy->getNumParams()) ||
416 (FTy->isVarArg() && NumArgs > FTy->getNumParams()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000417 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000418
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000419 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000420 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000421 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000422 "Invoking a function with a bad signature!");
423
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000424 OL[i+3].init(Args[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000425 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000426}
427
Misha Brukmanb1c93172005-04-21 23:48:37 +0000428InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000429 : TerminatorInst(II.getType(), Instruction::Invoke,
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000430 new Use[II.getNumOperands()], II.getNumOperands()),
431 ParamAttrs(0) {
432 setParamAttrs(II.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000433 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000434 Use *OL = OperandList, *InOL = II.OperandList;
435 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
436 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000437}
438
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000439BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
440 return getSuccessor(idx);
441}
442unsigned InvokeInst::getNumSuccessorsV() const {
443 return getNumSuccessors();
444}
445void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
446 return setSuccessor(idx, B);
447}
448
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000449void InvokeInst::setParamAttrs(const ParamAttrsList *newAttrs) {
450 if (ParamAttrs == newAttrs)
451 return;
452
Reid Spencerc6a83842007-04-22 17:28:03 +0000453 if (ParamAttrs)
454 ParamAttrs->dropRef();
455
456 if (newAttrs)
457 newAttrs->addRef();
458
459 ParamAttrs = newAttrs;
460}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000461
Duncan Sands5208d1a2007-11-28 17:07:01 +0000462bool InvokeInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
463 if (ParamAttrs && ParamAttrs->paramHasAttr(i, attr))
464 return true;
465 const Function *F = getCalledFunction();
466 return F && F->getParamAttrs() && F->getParamAttrs()->paramHasAttr(i, attr);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000467}
468
Duncan Sands5208d1a2007-11-28 17:07:01 +0000469
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000470//===----------------------------------------------------------------------===//
471// ReturnInst Implementation
472//===----------------------------------------------------------------------===//
473
Chris Lattner2195fc42007-02-24 00:55:48 +0000474ReturnInst::ReturnInst(const ReturnInst &RI)
475 : TerminatorInst(Type::VoidTy, Instruction::Ret,
476 &RetVal, RI.getNumOperands()) {
477 if (RI.getNumOperands())
478 RetVal.init(RI.RetVal, this);
479}
480
481ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
482 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) {
483 init(retVal);
484}
485ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
486 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
487 init(retVal);
488}
489ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
490 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
491}
492
493
494
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000495void ReturnInst::init(Value *retVal) {
496 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000497 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000498 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000499 NumOperands = 1;
500 RetVal.init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000501 }
502}
503
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000504unsigned ReturnInst::getNumSuccessorsV() const {
505 return getNumSuccessors();
506}
507
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000508// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
509// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000510void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000511 assert(0 && "ReturnInst has no successors!");
512}
513
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000514BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
515 assert(0 && "ReturnInst has no successors!");
516 abort();
517 return 0;
518}
519
520
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000521//===----------------------------------------------------------------------===//
522// UnwindInst Implementation
523//===----------------------------------------------------------------------===//
524
Chris Lattner2195fc42007-02-24 00:55:48 +0000525UnwindInst::UnwindInst(Instruction *InsertBefore)
526 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
527}
528UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
529 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
530}
531
532
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000533unsigned UnwindInst::getNumSuccessorsV() const {
534 return getNumSuccessors();
535}
536
537void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000538 assert(0 && "UnwindInst has no successors!");
539}
540
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000541BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
542 assert(0 && "UnwindInst has no successors!");
543 abort();
544 return 0;
545}
546
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000547//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000548// UnreachableInst Implementation
549//===----------------------------------------------------------------------===//
550
Chris Lattner2195fc42007-02-24 00:55:48 +0000551UnreachableInst::UnreachableInst(Instruction *InsertBefore)
552 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
553}
554UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
555 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
556}
557
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000558unsigned UnreachableInst::getNumSuccessorsV() const {
559 return getNumSuccessors();
560}
561
562void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
563 assert(0 && "UnwindInst has no successors!");
564}
565
566BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
567 assert(0 && "UnwindInst has no successors!");
568 abort();
569 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000570}
571
572//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000573// BranchInst Implementation
574//===----------------------------------------------------------------------===//
575
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000576void BranchInst::AssertOK() {
577 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000578 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000579 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000580}
581
Chris Lattner2195fc42007-02-24 00:55:48 +0000582BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
583 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) {
584 assert(IfTrue != 0 && "Branch destination may not be null!");
585 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
586}
587BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
588 Instruction *InsertBefore)
589: TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) {
590 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
591 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
592 Ops[2].init(Cond, this);
593#ifndef NDEBUG
594 AssertOK();
595#endif
596}
597
598BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
599 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) {
600 assert(IfTrue != 0 && "Branch destination may not be null!");
601 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
602}
603
604BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
605 BasicBlock *InsertAtEnd)
606 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) {
607 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
608 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
609 Ops[2].init(Cond, this);
610#ifndef NDEBUG
611 AssertOK();
612#endif
613}
614
615
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000616BranchInst::BranchInst(const BranchInst &BI) :
Chris Lattner2195fc42007-02-24 00:55:48 +0000617 TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000618 OperandList[0].init(BI.getOperand(0), this);
619 if (BI.getNumOperands() != 1) {
620 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
621 OperandList[1].init(BI.getOperand(1), this);
622 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000623 }
624}
625
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000626BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
627 return getSuccessor(idx);
628}
629unsigned BranchInst::getNumSuccessorsV() const {
630 return getNumSuccessors();
631}
632void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
633 setSuccessor(idx, B);
634}
635
636
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000637//===----------------------------------------------------------------------===//
638// AllocationInst Implementation
639//===----------------------------------------------------------------------===//
640
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000641static Value *getAISize(Value *Amt) {
642 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000643 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000644 else {
645 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000646 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000647 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000648 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000649 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000650 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000651}
652
Misha Brukmanb1c93172005-04-21 23:48:37 +0000653AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000654 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000655 Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000656 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000657 InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000658 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000659 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000660 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000661}
662
Misha Brukmanb1c93172005-04-21 23:48:37 +0000663AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000664 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000665 BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000666 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000667 InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000668 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000669 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000670 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000671}
672
Chris Lattner1c12a882006-06-21 16:53:47 +0000673// Out of line virtual method, so the vtable, etc has a home.
674AllocationInst::~AllocationInst() {
675}
676
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000677bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000678 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
679 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000680 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000681}
682
683const Type *AllocationInst::getAllocatedType() const {
684 return getType()->getElementType();
685}
686
687AllocaInst::AllocaInst(const AllocaInst &AI)
688 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000689 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000690}
691
692MallocInst::MallocInst(const MallocInst &MI)
693 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000694 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000695}
696
697//===----------------------------------------------------------------------===//
698// FreeInst Implementation
699//===----------------------------------------------------------------------===//
700
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000701void FreeInst::AssertOK() {
702 assert(isa<PointerType>(getOperand(0)->getType()) &&
703 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000704}
705
706FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000707 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000708 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000709}
710
711FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000712 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000713 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000714}
715
716
717//===----------------------------------------------------------------------===//
718// LoadInst Implementation
719//===----------------------------------------------------------------------===//
720
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000721void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000722 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000723 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000724}
725
726LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000727 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000728 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000729 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000730 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000731 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000732 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000733}
734
735LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000736 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000737 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000738 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000739 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000740 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000741 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000742}
743
744LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
745 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000746 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000747 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000748 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000749 setAlignment(0);
750 AssertOK();
751 setName(Name);
752}
753
754LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
755 unsigned Align, Instruction *InsertBef)
756 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
757 Load, Ptr, InsertBef) {
758 setVolatile(isVolatile);
759 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000760 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000761 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000762}
763
Dan Gohman68659282007-07-18 20:51:11 +0000764LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
765 unsigned Align, BasicBlock *InsertAE)
766 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
767 Load, Ptr, InsertAE) {
768 setVolatile(isVolatile);
769 setAlignment(Align);
770 AssertOK();
771 setName(Name);
772}
773
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000774LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
775 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000776 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000777 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000778 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000779 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000780 AssertOK();
781 setName(Name);
782}
783
784
785
786LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000787 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
788 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000789 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000790 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000791 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000792 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000793}
794
795LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000796 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
797 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000798 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000799 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000800 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000801 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000802}
803
804LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
805 Instruction *InsertBef)
806: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000807 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000808 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000809 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000810 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000811 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000812}
813
814LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
815 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000816 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
817 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000818 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000819 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000820 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000821 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000822}
823
Christopher Lamb84485702007-04-22 19:24:39 +0000824void LoadInst::setAlignment(unsigned Align) {
825 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
826 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
827}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000828
829//===----------------------------------------------------------------------===//
830// StoreInst Implementation
831//===----------------------------------------------------------------------===//
832
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000833void StoreInst::AssertOK() {
834 assert(isa<PointerType>(getOperand(1)->getType()) &&
835 "Ptr must have pointer type!");
836 assert(getOperand(0)->getType() ==
837 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000838 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000839}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000840
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000841
842StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000843 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000844 Ops[0].init(val, this);
845 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000846 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000847 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000848 AssertOK();
849}
850
851StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000852 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000853 Ops[0].init(val, this);
854 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000855 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000856 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000857 AssertOK();
858}
859
Misha Brukmanb1c93172005-04-21 23:48:37 +0000860StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000861 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000862 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000863 Ops[0].init(val, this);
864 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000865 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000866 setAlignment(0);
867 AssertOK();
868}
869
870StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
871 unsigned Align, Instruction *InsertBefore)
872 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
873 Ops[0].init(val, this);
874 Ops[1].init(addr, this);
875 setVolatile(isVolatile);
876 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000877 AssertOK();
878}
879
Misha Brukmanb1c93172005-04-21 23:48:37 +0000880StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000881 unsigned Align, BasicBlock *InsertAtEnd)
882 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
883 Ops[0].init(val, this);
884 Ops[1].init(addr, this);
885 setVolatile(isVolatile);
886 setAlignment(Align);
887 AssertOK();
888}
889
890StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000891 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000892 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000893 Ops[0].init(val, this);
894 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000895 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000896 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000897 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000898}
899
Christopher Lamb84485702007-04-22 19:24:39 +0000900void StoreInst::setAlignment(unsigned Align) {
901 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
902 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
903}
904
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000905//===----------------------------------------------------------------------===//
906// GetElementPtrInst Implementation
907//===----------------------------------------------------------------------===//
908
Chris Lattner79807c3d2007-01-31 19:47:18 +0000909void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
910 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000911 Use *OL = OperandList = new Use[NumOperands];
912 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000913
Chris Lattner79807c3d2007-01-31 19:47:18 +0000914 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000915 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000916}
917
Chris Lattner82981202005-05-03 05:43:30 +0000918void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
919 NumOperands = 2;
920 Use *OL = OperandList = new Use[2];
921 OL[0].init(Ptr, this);
922 OL[1].init(Idx, this);
923}
924
Chris Lattner82981202005-05-03 05:43:30 +0000925GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
926 const std::string &Name, Instruction *InBe)
Chris Lattner2195fc42007-02-24 00:55:48 +0000927 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
928 GetElementPtr, 0, 0, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +0000929 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000930 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000931}
932
933GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
934 const std::string &Name, BasicBlock *IAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000935 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
936 GetElementPtr, 0, 0, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +0000937 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000938 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000939}
940
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000941GetElementPtrInst::~GetElementPtrInst() {
942 delete[] OperandList;
943}
944
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000945// getIndexedType - Returns the type of the element that would be loaded with
946// a load instruction with the specified parameters.
947//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000948// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000949// pointer type.
950//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000951const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +0000952 Value* const *Idxs,
953 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000954 bool AllowCompositeLeaf) {
955 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
956
957 // Handle the special case of the empty set index set...
Chris Lattner302116a2007-01-31 04:40:28 +0000958 if (NumIdx == 0)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000959 if (AllowCompositeLeaf ||
960 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
961 return cast<PointerType>(Ptr)->getElementType();
962 else
963 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000964
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000965 unsigned CurIdx = 0;
966 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +0000967 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000968 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
969 return 0; // Can't load a whole structure or array!?!?
970 }
971
Chris Lattner302116a2007-01-31 04:40:28 +0000972 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000973 if (isa<PointerType>(CT) && CurIdx != 1)
974 return 0; // Can only index into pointer types at the first index!
975 if (!CT->indexValid(Index)) return 0;
976 Ptr = CT->getTypeAtIndex(Index);
977
978 // If the new type forwards to another type, then it is in the middle
979 // of being refined to another type (and hence, may have dropped all
980 // references to what it was using before). So, use the new forwarded
981 // type.
982 if (const Type * Ty = Ptr->getForwardedType()) {
983 Ptr = Ty;
984 }
985 }
Chris Lattner302116a2007-01-31 04:40:28 +0000986 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000987}
988
Chris Lattner82981202005-05-03 05:43:30 +0000989const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
990 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
991 if (!PTy) return 0; // Type isn't a pointer type!
992
993 // Check the pointer index.
994 if (!PTy->indexValid(Idx)) return 0;
995
Chris Lattnerc2233332005-05-03 16:44:45 +0000996 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +0000997}
998
Chris Lattner45f15572007-04-14 00:12:57 +0000999
1000/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1001/// zeros. If so, the result pointer and the first operand have the same
1002/// value, just potentially different types.
1003bool GetElementPtrInst::hasAllZeroIndices() const {
1004 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1005 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1006 if (!CI->isZero()) return false;
1007 } else {
1008 return false;
1009 }
1010 }
1011 return true;
1012}
1013
Chris Lattner27058292007-04-27 20:35:56 +00001014/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1015/// constant integers. If so, the result pointer and the first operand have
1016/// a constant offset between them.
1017bool GetElementPtrInst::hasAllConstantIndices() const {
1018 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1019 if (!isa<ConstantInt>(getOperand(i)))
1020 return false;
1021 }
1022 return true;
1023}
1024
Chris Lattner45f15572007-04-14 00:12:57 +00001025
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001026//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001027// ExtractElementInst Implementation
1028//===----------------------------------------------------------------------===//
1029
1030ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001031 const std::string &Name,
1032 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001033 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001034 ExtractElement, Ops, 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001035 assert(isValidOperands(Val, Index) &&
1036 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +00001037 Ops[0].init(Val, this);
1038 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001039 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001040}
1041
Chris Lattner65511ff2006-10-05 06:24:58 +00001042ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1043 const std::string &Name,
1044 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001045 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001046 ExtractElement, Ops, 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001047 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001048 assert(isValidOperands(Val, Index) &&
1049 "Invalid extractelement instruction operands!");
1050 Ops[0].init(Val, this);
1051 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001052 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001053}
1054
1055
Robert Bocchino23004482006-01-10 19:05:34 +00001056ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001057 const std::string &Name,
1058 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001059 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001060 ExtractElement, Ops, 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001061 assert(isValidOperands(Val, Index) &&
1062 "Invalid extractelement instruction operands!");
1063
Robert Bocchino23004482006-01-10 19:05:34 +00001064 Ops[0].init(Val, this);
1065 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001066 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001067}
1068
Chris Lattner65511ff2006-10-05 06:24:58 +00001069ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1070 const std::string &Name,
1071 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001072 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001073 ExtractElement, Ops, 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001074 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001075 assert(isValidOperands(Val, Index) &&
1076 "Invalid extractelement instruction operands!");
1077
1078 Ops[0].init(Val, this);
1079 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001080 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001081}
1082
1083
Chris Lattner54865b32006-04-08 04:05:48 +00001084bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001085 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001086 return false;
1087 return true;
1088}
1089
1090
Robert Bocchino23004482006-01-10 19:05:34 +00001091//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001092// InsertElementInst Implementation
1093//===----------------------------------------------------------------------===//
1094
Chris Lattner0875d942006-04-14 22:20:32 +00001095InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1096 : Instruction(IE.getType(), InsertElement, Ops, 3) {
1097 Ops[0].init(IE.Ops[0], this);
1098 Ops[1].init(IE.Ops[1], this);
1099 Ops[2].init(IE.Ops[2], this);
1100}
Chris Lattner54865b32006-04-08 04:05:48 +00001101InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001102 const std::string &Name,
1103 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001104 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001105 assert(isValidOperands(Vec, Elt, Index) &&
1106 "Invalid insertelement instruction operands!");
1107 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001108 Ops[1].init(Elt, this);
1109 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001110 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001111}
1112
Chris Lattner65511ff2006-10-05 06:24:58 +00001113InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1114 const std::string &Name,
1115 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001116 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001117 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001118 assert(isValidOperands(Vec, Elt, Index) &&
1119 "Invalid insertelement instruction operands!");
1120 Ops[0].init(Vec, this);
1121 Ops[1].init(Elt, this);
1122 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001123 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001124}
1125
1126
Chris Lattner54865b32006-04-08 04:05:48 +00001127InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001128 const std::string &Name,
1129 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001130 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001131 assert(isValidOperands(Vec, Elt, Index) &&
1132 "Invalid insertelement instruction operands!");
1133
1134 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001135 Ops[1].init(Elt, this);
1136 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001137 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001138}
1139
Chris Lattner65511ff2006-10-05 06:24:58 +00001140InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1141 const std::string &Name,
1142 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001143: Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001144 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001145 assert(isValidOperands(Vec, Elt, Index) &&
1146 "Invalid insertelement instruction operands!");
1147
1148 Ops[0].init(Vec, this);
1149 Ops[1].init(Elt, this);
1150 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001151 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001152}
1153
Chris Lattner54865b32006-04-08 04:05:48 +00001154bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1155 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001156 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001157 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001158
Reid Spencerd84d35b2007-02-15 02:26:10 +00001159 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001160 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001161
Reid Spencer8d9336d2006-12-31 05:26:44 +00001162 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001163 return false; // Third operand of insertelement must be uint.
1164 return true;
1165}
1166
1167
Robert Bocchinoca27f032006-01-17 20:07:22 +00001168//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001169// ShuffleVectorInst Implementation
1170//===----------------------------------------------------------------------===//
1171
Chris Lattner0875d942006-04-14 22:20:32 +00001172ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1173 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
1174 Ops[0].init(SV.Ops[0], this);
1175 Ops[1].init(SV.Ops[1], this);
1176 Ops[2].init(SV.Ops[2], this);
1177}
1178
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001179ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1180 const std::string &Name,
1181 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00001182 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001183 assert(isValidOperands(V1, V2, Mask) &&
1184 "Invalid shuffle vector instruction operands!");
1185 Ops[0].init(V1, this);
1186 Ops[1].init(V2, this);
1187 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001188 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001189}
1190
1191ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1192 const std::string &Name,
1193 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00001194 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001195 assert(isValidOperands(V1, V2, Mask) &&
1196 "Invalid shuffle vector instruction operands!");
1197
1198 Ops[0].init(V1, this);
1199 Ops[1].init(V2, this);
1200 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001201 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001202}
1203
1204bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1205 const Value *Mask) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001206 if (!isa<VectorType>(V1->getType())) return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001207 if (V1->getType() != V2->getType()) return false;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001208 if (!isa<VectorType>(Mask->getType()) ||
1209 cast<VectorType>(Mask->getType())->getElementType() != Type::Int32Ty ||
1210 cast<VectorType>(Mask->getType())->getNumElements() !=
1211 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001212 return false;
1213 return true;
1214}
1215
1216
1217//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001218// BinaryOperator Class
1219//===----------------------------------------------------------------------===//
1220
Chris Lattner2195fc42007-02-24 00:55:48 +00001221BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1222 const Type *Ty, const std::string &Name,
1223 Instruction *InsertBefore)
1224 : Instruction(Ty, iType, Ops, 2, InsertBefore) {
1225 Ops[0].init(S1, this);
1226 Ops[1].init(S2, this);
1227 init(iType);
1228 setName(Name);
1229}
1230
1231BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1232 const Type *Ty, const std::string &Name,
1233 BasicBlock *InsertAtEnd)
1234 : Instruction(Ty, iType, Ops, 2, InsertAtEnd) {
1235 Ops[0].init(S1, this);
1236 Ops[1].init(S2, this);
1237 init(iType);
1238 setName(Name);
1239}
1240
1241
1242void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001243 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001244 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001245 assert(LHS->getType() == RHS->getType() &&
1246 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001247#ifndef NDEBUG
1248 switch (iType) {
1249 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001250 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001251 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001252 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001253 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001254 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001255 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001256 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001257 case UDiv:
1258 case SDiv:
1259 assert(getType() == LHS->getType() &&
1260 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001261 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1262 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001263 "Incorrect operand type (not integer) for S/UDIV");
1264 break;
1265 case FDiv:
1266 assert(getType() == LHS->getType() &&
1267 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001268 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1269 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001270 && "Incorrect operand type (not floating point) for FDIV");
1271 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001272 case URem:
1273 case SRem:
1274 assert(getType() == LHS->getType() &&
1275 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001276 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1277 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001278 "Incorrect operand type (not integer) for S/UREM");
1279 break;
1280 case FRem:
1281 assert(getType() == LHS->getType() &&
1282 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001283 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1284 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001285 && "Incorrect operand type (not floating point) for FREM");
1286 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001287 case Shl:
1288 case LShr:
1289 case AShr:
1290 assert(getType() == LHS->getType() &&
1291 "Shift operation should return same type as operands!");
1292 assert(getType()->isInteger() &&
1293 "Shift operation requires integer operands");
1294 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001295 case And: case Or:
1296 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001297 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001298 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001299 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001300 (isa<VectorType>(getType()) &&
1301 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001302 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001303 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001304 default:
1305 break;
1306 }
1307#endif
1308}
1309
1310BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001311 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001312 Instruction *InsertBefore) {
1313 assert(S1->getType() == S2->getType() &&
1314 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001315 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001316}
1317
1318BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001319 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001320 BasicBlock *InsertAtEnd) {
1321 BinaryOperator *Res = create(Op, S1, S2, Name);
1322 InsertAtEnd->getInstList().push_back(Res);
1323 return Res;
1324}
1325
1326BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1327 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001328 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1329 return new BinaryOperator(Instruction::Sub,
1330 zero, Op,
1331 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001332}
1333
1334BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1335 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001336 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1337 return new BinaryOperator(Instruction::Sub,
1338 zero, Op,
1339 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001340}
1341
1342BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1343 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001344 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001345 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001346 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001347 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001348 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001349 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001350 }
1351
1352 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001353 Op->getType(), Name, InsertBefore);
1354}
1355
1356BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1357 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001358 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001359 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001360 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001361 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001362 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001363 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001364 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001365 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001366 }
1367
1368 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001369 Op->getType(), Name, InsertAtEnd);
1370}
1371
1372
1373// isConstantAllOnes - Helper function for several functions below
1374static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001375 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1376 return CI->isAllOnesValue();
1377 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1378 return CV->isAllOnesValue();
1379 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001380}
1381
1382bool BinaryOperator::isNeg(const Value *V) {
1383 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1384 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001385 return Bop->getOperand(0) ==
1386 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001387 return false;
1388}
1389
1390bool BinaryOperator::isNot(const Value *V) {
1391 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1392 return (Bop->getOpcode() == Instruction::Xor &&
1393 (isConstantAllOnes(Bop->getOperand(1)) ||
1394 isConstantAllOnes(Bop->getOperand(0))));
1395 return false;
1396}
1397
Chris Lattner2c7d1772005-04-24 07:28:37 +00001398Value *BinaryOperator::getNegArgument(Value *BinOp) {
1399 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1400 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001401}
1402
Chris Lattner2c7d1772005-04-24 07:28:37 +00001403const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1404 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001405}
1406
Chris Lattner2c7d1772005-04-24 07:28:37 +00001407Value *BinaryOperator::getNotArgument(Value *BinOp) {
1408 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1409 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1410 Value *Op0 = BO->getOperand(0);
1411 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001412 if (isConstantAllOnes(Op0)) return Op1;
1413
1414 assert(isConstantAllOnes(Op1));
1415 return Op0;
1416}
1417
Chris Lattner2c7d1772005-04-24 07:28:37 +00001418const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1419 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001420}
1421
1422
1423// swapOperands - Exchange the two operands to this instruction. This
1424// instruction is safe to use on any binary instruction and does not
1425// modify the semantics of the instruction. If the instruction is
1426// order dependent (SetLT f.e.) the opcode is changed.
1427//
1428bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001429 if (!isCommutative())
1430 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001431 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001432 return false;
1433}
1434
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001435//===----------------------------------------------------------------------===//
1436// CastInst Class
1437//===----------------------------------------------------------------------===//
1438
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001439// Just determine if this cast only deals with integral->integral conversion.
1440bool CastInst::isIntegerCast() const {
1441 switch (getOpcode()) {
1442 default: return false;
1443 case Instruction::ZExt:
1444 case Instruction::SExt:
1445 case Instruction::Trunc:
1446 return true;
1447 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001448 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001449 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001450}
1451
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001452bool CastInst::isLosslessCast() const {
1453 // Only BitCast can be lossless, exit fast if we're not BitCast
1454 if (getOpcode() != Instruction::BitCast)
1455 return false;
1456
1457 // Identity cast is always lossless
1458 const Type* SrcTy = getOperand(0)->getType();
1459 const Type* DstTy = getType();
1460 if (SrcTy == DstTy)
1461 return true;
1462
Reid Spencer8d9336d2006-12-31 05:26:44 +00001463 // Pointer to pointer is always lossless.
1464 if (isa<PointerType>(SrcTy))
1465 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001466 return false; // Other types have no identity values
1467}
1468
1469/// This function determines if the CastInst does not require any bits to be
1470/// changed in order to effect the cast. Essentially, it identifies cases where
1471/// no code gen is necessary for the cast, hence the name no-op cast. For
1472/// example, the following are all no-op casts:
1473/// # bitcast uint %X, int
1474/// # bitcast uint* %x, sbyte*
Dan Gohmanfead7972007-05-11 21:43:24 +00001475/// # bitcast vector< 2 x int > %x, vector< 4 x short>
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001476/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1477/// @brief Determine if a cast is a no-op.
1478bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1479 switch (getOpcode()) {
1480 default:
1481 assert(!"Invalid CastOp");
1482 case Instruction::Trunc:
1483 case Instruction::ZExt:
1484 case Instruction::SExt:
1485 case Instruction::FPTrunc:
1486 case Instruction::FPExt:
1487 case Instruction::UIToFP:
1488 case Instruction::SIToFP:
1489 case Instruction::FPToUI:
1490 case Instruction::FPToSI:
1491 return false; // These always modify bits
1492 case Instruction::BitCast:
1493 return true; // BitCast never modifies bits.
1494 case Instruction::PtrToInt:
1495 return IntPtrTy->getPrimitiveSizeInBits() ==
1496 getType()->getPrimitiveSizeInBits();
1497 case Instruction::IntToPtr:
1498 return IntPtrTy->getPrimitiveSizeInBits() ==
1499 getOperand(0)->getType()->getPrimitiveSizeInBits();
1500 }
1501}
1502
1503/// This function determines if a pair of casts can be eliminated and what
1504/// opcode should be used in the elimination. This assumes that there are two
1505/// instructions like this:
1506/// * %F = firstOpcode SrcTy %x to MidTy
1507/// * %S = secondOpcode MidTy %F to DstTy
1508/// The function returns a resultOpcode so these two casts can be replaced with:
1509/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1510/// If no such cast is permited, the function returns 0.
1511unsigned CastInst::isEliminableCastPair(
1512 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1513 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1514{
1515 // Define the 144 possibilities for these two cast instructions. The values
1516 // in this matrix determine what to do in a given situation and select the
1517 // case in the switch below. The rows correspond to firstOp, the columns
1518 // correspond to secondOp. In looking at the table below, keep in mind
1519 // the following cast properties:
1520 //
1521 // Size Compare Source Destination
1522 // Operator Src ? Size Type Sign Type Sign
1523 // -------- ------------ ------------------- ---------------------
1524 // TRUNC > Integer Any Integral Any
1525 // ZEXT < Integral Unsigned Integer Any
1526 // SEXT < Integral Signed Integer Any
1527 // FPTOUI n/a FloatPt n/a Integral Unsigned
1528 // FPTOSI n/a FloatPt n/a Integral Signed
1529 // UITOFP n/a Integral Unsigned FloatPt n/a
1530 // SITOFP n/a Integral Signed FloatPt n/a
1531 // FPTRUNC > FloatPt n/a FloatPt n/a
1532 // FPEXT < FloatPt n/a FloatPt n/a
1533 // PTRTOINT n/a Pointer n/a Integral Unsigned
1534 // INTTOPTR n/a Integral Unsigned Pointer n/a
1535 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001536 //
1537 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1538 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1539 // into "fptoui double to ulong", but this loses information about the range
1540 // of the produced value (we no longer know the top-part is all zeros).
1541 // Further this conversion is often much more expensive for typical hardware,
1542 // and causes issues when building libgcc. We disallow fptosi+sext for the
1543 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001544 const unsigned numCastOps =
1545 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1546 static const uint8_t CastResults[numCastOps][numCastOps] = {
1547 // T F F U S F F P I B -+
1548 // R Z S P P I I T P 2 N T |
1549 // U E E 2 2 2 2 R E I T C +- secondOp
1550 // N X X U S F F N X N 2 V |
1551 // C T T I I P P C T T P T -+
1552 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1553 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1554 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001555 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1556 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001557 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1558 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1559 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1560 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1561 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1562 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1563 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1564 };
1565
1566 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1567 [secondOp-Instruction::CastOpsBegin];
1568 switch (ElimCase) {
1569 case 0:
1570 // categorically disallowed
1571 return 0;
1572 case 1:
1573 // allowed, use first cast's opcode
1574 return firstOp;
1575 case 2:
1576 // allowed, use second cast's opcode
1577 return secondOp;
1578 case 3:
1579 // no-op cast in second op implies firstOp as long as the DestTy
1580 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001581 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001582 return firstOp;
1583 return 0;
1584 case 4:
1585 // no-op cast in second op implies firstOp as long as the DestTy
1586 // is floating point
1587 if (DstTy->isFloatingPoint())
1588 return firstOp;
1589 return 0;
1590 case 5:
1591 // no-op cast in first op implies secondOp as long as the SrcTy
1592 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001593 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001594 return secondOp;
1595 return 0;
1596 case 6:
1597 // no-op cast in first op implies secondOp as long as the SrcTy
1598 // is a floating point
1599 if (SrcTy->isFloatingPoint())
1600 return secondOp;
1601 return 0;
1602 case 7: {
1603 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1604 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1605 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1606 if (MidSize >= PtrSize)
1607 return Instruction::BitCast;
1608 return 0;
1609 }
1610 case 8: {
1611 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1612 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1613 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1614 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1615 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1616 if (SrcSize == DstSize)
1617 return Instruction::BitCast;
1618 else if (SrcSize < DstSize)
1619 return firstOp;
1620 return secondOp;
1621 }
1622 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1623 return Instruction::ZExt;
1624 case 10:
1625 // fpext followed by ftrunc is allowed if the bit size returned to is
1626 // the same as the original, in which case its just a bitcast
1627 if (SrcTy == DstTy)
1628 return Instruction::BitCast;
1629 return 0; // If the types are not the same we can't eliminate it.
1630 case 11:
1631 // bitcast followed by ptrtoint is allowed as long as the bitcast
1632 // is a pointer to pointer cast.
1633 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1634 return secondOp;
1635 return 0;
1636 case 12:
1637 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1638 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1639 return firstOp;
1640 return 0;
1641 case 13: {
1642 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1643 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1644 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1645 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1646 if (SrcSize <= PtrSize && SrcSize == DstSize)
1647 return Instruction::BitCast;
1648 return 0;
1649 }
1650 case 99:
1651 // cast combination can't happen (error in input). This is for all cases
1652 // where the MidTy is not the same for the two cast instructions.
1653 assert(!"Invalid Cast Combination");
1654 return 0;
1655 default:
1656 assert(!"Error in CastResults table!!!");
1657 return 0;
1658 }
1659 return 0;
1660}
1661
1662CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1663 const std::string &Name, Instruction *InsertBefore) {
1664 // Construct and return the appropriate CastInst subclass
1665 switch (op) {
1666 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1667 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1668 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1669 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1670 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1671 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1672 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1673 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1674 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1675 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1676 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1677 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1678 default:
1679 assert(!"Invalid opcode provided");
1680 }
1681 return 0;
1682}
1683
1684CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1685 const std::string &Name, BasicBlock *InsertAtEnd) {
1686 // Construct and return the appropriate CastInst subclass
1687 switch (op) {
1688 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1689 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1690 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1691 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1692 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1693 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1694 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1695 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1696 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1697 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1698 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1699 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1700 default:
1701 assert(!"Invalid opcode provided");
1702 }
1703 return 0;
1704}
1705
Reid Spencer5c140882006-12-04 20:17:56 +00001706CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1707 const std::string &Name,
1708 Instruction *InsertBefore) {
1709 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1710 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1711 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1712}
1713
1714CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1715 const std::string &Name,
1716 BasicBlock *InsertAtEnd) {
1717 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1718 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1719 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1720}
1721
1722CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1723 const std::string &Name,
1724 Instruction *InsertBefore) {
1725 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1726 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1727 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1728}
1729
1730CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1731 const std::string &Name,
1732 BasicBlock *InsertAtEnd) {
1733 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1734 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1735 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1736}
1737
1738CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1739 const std::string &Name,
1740 Instruction *InsertBefore) {
1741 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1742 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1743 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1744}
1745
1746CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1747 const std::string &Name,
1748 BasicBlock *InsertAtEnd) {
1749 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1750 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1751 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1752}
1753
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001754CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1755 const std::string &Name,
1756 BasicBlock *InsertAtEnd) {
1757 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001758 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001759 "Invalid cast");
1760
Chris Lattner03c49532007-01-15 02:27:26 +00001761 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001762 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1763 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1764}
1765
1766/// @brief Create a BitCast or a PtrToInt cast instruction
1767CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1768 const std::string &Name,
1769 Instruction *InsertBefore) {
1770 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001771 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001772 "Invalid cast");
1773
Chris Lattner03c49532007-01-15 02:27:26 +00001774 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001775 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1776 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1777}
1778
Reid Spencer7e933472006-12-12 00:49:44 +00001779CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1780 bool isSigned, const std::string &Name,
1781 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001782 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001783 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1784 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1785 Instruction::CastOps opcode =
1786 (SrcBits == DstBits ? Instruction::BitCast :
1787 (SrcBits > DstBits ? Instruction::Trunc :
1788 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1789 return create(opcode, C, Ty, Name, InsertBefore);
1790}
1791
1792CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1793 bool isSigned, const std::string &Name,
1794 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001795 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001796 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1797 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1798 Instruction::CastOps opcode =
1799 (SrcBits == DstBits ? Instruction::BitCast :
1800 (SrcBits > DstBits ? Instruction::Trunc :
1801 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1802 return create(opcode, C, Ty, Name, InsertAtEnd);
1803}
1804
1805CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1806 const std::string &Name,
1807 Instruction *InsertBefore) {
1808 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1809 "Invalid cast");
1810 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1811 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1812 Instruction::CastOps opcode =
1813 (SrcBits == DstBits ? Instruction::BitCast :
1814 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1815 return create(opcode, C, Ty, Name, InsertBefore);
1816}
1817
1818CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1819 const std::string &Name,
1820 BasicBlock *InsertAtEnd) {
1821 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1822 "Invalid cast");
1823 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1824 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1825 Instruction::CastOps opcode =
1826 (SrcBits == DstBits ? Instruction::BitCast :
1827 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1828 return create(opcode, C, Ty, Name, InsertAtEnd);
1829}
1830
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001831// Provide a way to get a "cast" where the cast opcode is inferred from the
1832// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001833// logic in the castIsValid function below. This axiom should hold:
1834// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
1835// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001836// casting opcode for the arguments passed to it.
1837Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00001838CastInst::getCastOpcode(
1839 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001840 // Get the bit sizes, we'll need these
1841 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00001842 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1843 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001844
1845 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00001846 if (DestTy->isInteger()) { // Casting to integral
1847 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001848 if (DestBits < SrcBits)
1849 return Trunc; // int -> smaller int
1850 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00001851 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001852 return SExt; // signed -> SEXT
1853 else
1854 return ZExt; // unsigned -> ZEXT
1855 } else {
1856 return BitCast; // Same size, No-op cast
1857 }
1858 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00001859 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001860 return FPToSI; // FP -> sint
1861 else
1862 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00001863 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001864 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00001865 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001866 return BitCast; // Same size, no-op cast
1867 } else {
1868 assert(isa<PointerType>(SrcTy) &&
1869 "Casting from a value that is not first-class type");
1870 return PtrToInt; // ptr -> int
1871 }
1872 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00001873 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00001874 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001875 return SIToFP; // sint -> FP
1876 else
1877 return UIToFP; // uint -> FP
1878 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1879 if (DestBits < SrcBits) {
1880 return FPTrunc; // FP -> smaller FP
1881 } else if (DestBits > SrcBits) {
1882 return FPExt; // FP -> larger FP
1883 } else {
1884 return BitCast; // same size, no-op cast
1885 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001886 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001887 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00001888 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001889 return BitCast; // same size, no-op cast
1890 } else {
1891 assert(0 && "Casting pointer or non-first class to float");
1892 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001893 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
1894 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001895 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00001896 "Casting vector to vector of different widths");
1897 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001898 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00001899 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001900 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00001901 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001902 }
1903 } else if (isa<PointerType>(DestTy)) {
1904 if (isa<PointerType>(SrcTy)) {
1905 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00001906 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001907 return IntToPtr; // int -> ptr
1908 } else {
1909 assert(!"Casting pointer to other than pointer or int");
1910 }
1911 } else {
1912 assert(!"Casting to type that is not first-class");
1913 }
1914
1915 // If we fall through to here we probably hit an assertion cast above
1916 // and assertions are not turned on. Anything we return is an error, so
1917 // BitCast is as good a choice as any.
1918 return BitCast;
1919}
1920
1921//===----------------------------------------------------------------------===//
1922// CastInst SubClass Constructors
1923//===----------------------------------------------------------------------===//
1924
1925/// Check that the construction parameters for a CastInst are correct. This
1926/// could be broken out into the separate constructors but it is useful to have
1927/// it in one place and to eliminate the redundant code for getting the sizes
1928/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001929bool
1930CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001931
1932 // Check for type sanity on the arguments
1933 const Type *SrcTy = S->getType();
1934 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
1935 return false;
1936
1937 // Get the size of the types in bits, we'll need this later
1938 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1939 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
1940
1941 // Switch on the opcode provided
1942 switch (op) {
1943 default: return false; // This is an input error
1944 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00001945 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001946 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001947 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001948 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001949 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001950 case Instruction::FPTrunc:
1951 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1952 SrcBitSize > DstBitSize;
1953 case Instruction::FPExt:
1954 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1955 SrcBitSize < DstBitSize;
1956 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001957 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00001958 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
1959 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
1960 return SVTy->getElementType()->isInteger() &&
1961 DVTy->getElementType()->isFloatingPoint() &&
1962 SVTy->getNumElements() == DVTy->getNumElements();
1963 }
1964 }
Chris Lattner03c49532007-01-15 02:27:26 +00001965 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001966 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001967 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00001968 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
1969 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
1970 return SVTy->getElementType()->isFloatingPoint() &&
1971 DVTy->getElementType()->isInteger() &&
1972 SVTy->getNumElements() == DVTy->getNumElements();
1973 }
1974 }
Chris Lattner03c49532007-01-15 02:27:26 +00001975 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001976 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00001977 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001978 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00001979 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001980 case Instruction::BitCast:
1981 // BitCast implies a no-op cast of type only. No bits change.
1982 // However, you can't cast pointers to anything but pointers.
1983 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
1984 return false;
1985
1986 // Now we know we're not dealing with a pointer/non-poiner mismatch. In all
1987 // these cases, the cast is okay if the source and destination bit widths
1988 // are identical.
1989 return SrcBitSize == DstBitSize;
1990 }
1991}
1992
1993TruncInst::TruncInst(
1994 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1995) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001996 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001997}
1998
1999TruncInst::TruncInst(
2000 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2001) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002002 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002003}
2004
2005ZExtInst::ZExtInst(
2006 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2007) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002008 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002009}
2010
2011ZExtInst::ZExtInst(
2012 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2013) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002014 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002015}
2016SExtInst::SExtInst(
2017 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2018) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002019 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002020}
2021
Jeff Cohencc08c832006-12-02 02:22:01 +00002022SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002023 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2024) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002025 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002026}
2027
2028FPTruncInst::FPTruncInst(
2029 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2030) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002031 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002032}
2033
2034FPTruncInst::FPTruncInst(
2035 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2036) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002037 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002038}
2039
2040FPExtInst::FPExtInst(
2041 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2042) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002043 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002044}
2045
2046FPExtInst::FPExtInst(
2047 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2048) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002049 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002050}
2051
2052UIToFPInst::UIToFPInst(
2053 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2054) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002055 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002056}
2057
2058UIToFPInst::UIToFPInst(
2059 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2060) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002061 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002062}
2063
2064SIToFPInst::SIToFPInst(
2065 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2066) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002067 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002068}
2069
2070SIToFPInst::SIToFPInst(
2071 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2072) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002073 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002074}
2075
2076FPToUIInst::FPToUIInst(
2077 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2078) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002079 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002080}
2081
2082FPToUIInst::FPToUIInst(
2083 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2084) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002085 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002086}
2087
2088FPToSIInst::FPToSIInst(
2089 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2090) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002091 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002092}
2093
2094FPToSIInst::FPToSIInst(
2095 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2096) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002097 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002098}
2099
2100PtrToIntInst::PtrToIntInst(
2101 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2102) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002103 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002104}
2105
2106PtrToIntInst::PtrToIntInst(
2107 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2108) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002109 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002110}
2111
2112IntToPtrInst::IntToPtrInst(
2113 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2114) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002115 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002116}
2117
2118IntToPtrInst::IntToPtrInst(
2119 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2120) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002121 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002122}
2123
2124BitCastInst::BitCastInst(
2125 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2126) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002127 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002128}
2129
2130BitCastInst::BitCastInst(
2131 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2132) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002133 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002134}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002135
2136//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002137// CmpInst Classes
2138//===----------------------------------------------------------------------===//
2139
2140CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2141 const std::string &Name, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00002142 : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002143 Ops[0].init(LHS, this);
2144 Ops[1].init(RHS, this);
2145 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002146 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002147 if (op == Instruction::ICmp) {
2148 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2149 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2150 "Invalid ICmp predicate value");
2151 const Type* Op0Ty = getOperand(0)->getType();
2152 const Type* Op1Ty = getOperand(1)->getType();
2153 assert(Op0Ty == Op1Ty &&
2154 "Both operands to ICmp instruction are not of the same type!");
2155 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002156 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002157 "Invalid operand types for ICmp instruction");
2158 return;
2159 }
2160 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2161 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2162 "Invalid FCmp predicate value");
2163 const Type* Op0Ty = getOperand(0)->getType();
2164 const Type* Op1Ty = getOperand(1)->getType();
2165 assert(Op0Ty == Op1Ty &&
2166 "Both operands to FCmp instruction are not of the same type!");
2167 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002168 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002169 "Invalid operand types for FCmp instruction");
2170}
2171
2172CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2173 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00002174 : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002175 Ops[0].init(LHS, this);
2176 Ops[1].init(RHS, this);
2177 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002178 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002179 if (op == Instruction::ICmp) {
2180 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2181 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2182 "Invalid ICmp predicate value");
2183
2184 const Type* Op0Ty = getOperand(0)->getType();
2185 const Type* Op1Ty = getOperand(1)->getType();
2186 assert(Op0Ty == Op1Ty &&
2187 "Both operands to ICmp instruction are not of the same type!");
2188 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002189 assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002190 "Invalid operand types for ICmp instruction");
2191 return;
2192 }
2193 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2194 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2195 "Invalid FCmp predicate value");
2196 const Type* Op0Ty = getOperand(0)->getType();
2197 const Type* Op1Ty = getOperand(1)->getType();
2198 assert(Op0Ty == Op1Ty &&
2199 "Both operands to FCmp instruction are not of the same type!");
2200 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002201 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002202 "Invalid operand types for FCmp instruction");
2203}
2204
2205CmpInst *
2206CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2207 const std::string &Name, Instruction *InsertBefore) {
2208 if (Op == Instruction::ICmp) {
2209 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2210 InsertBefore);
2211 }
2212 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2213 InsertBefore);
2214}
2215
2216CmpInst *
2217CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2218 const std::string &Name, BasicBlock *InsertAtEnd) {
2219 if (Op == Instruction::ICmp) {
2220 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2221 InsertAtEnd);
2222 }
2223 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2224 InsertAtEnd);
2225}
2226
2227void CmpInst::swapOperands() {
2228 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2229 IC->swapOperands();
2230 else
2231 cast<FCmpInst>(this)->swapOperands();
2232}
2233
2234bool CmpInst::isCommutative() {
2235 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2236 return IC->isCommutative();
2237 return cast<FCmpInst>(this)->isCommutative();
2238}
2239
2240bool CmpInst::isEquality() {
2241 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2242 return IC->isEquality();
2243 return cast<FCmpInst>(this)->isEquality();
2244}
2245
2246
2247ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2248 switch (pred) {
2249 default:
2250 assert(!"Unknown icmp predicate!");
2251 case ICMP_EQ: return ICMP_NE;
2252 case ICMP_NE: return ICMP_EQ;
2253 case ICMP_UGT: return ICMP_ULE;
2254 case ICMP_ULT: return ICMP_UGE;
2255 case ICMP_UGE: return ICMP_ULT;
2256 case ICMP_ULE: return ICMP_UGT;
2257 case ICMP_SGT: return ICMP_SLE;
2258 case ICMP_SLT: return ICMP_SGE;
2259 case ICMP_SGE: return ICMP_SLT;
2260 case ICMP_SLE: return ICMP_SGT;
2261 }
2262}
2263
2264ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2265 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002266 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002267 case ICMP_EQ: case ICMP_NE:
2268 return pred;
2269 case ICMP_SGT: return ICMP_SLT;
2270 case ICMP_SLT: return ICMP_SGT;
2271 case ICMP_SGE: return ICMP_SLE;
2272 case ICMP_SLE: return ICMP_SGE;
2273 case ICMP_UGT: return ICMP_ULT;
2274 case ICMP_ULT: return ICMP_UGT;
2275 case ICMP_UGE: return ICMP_ULE;
2276 case ICMP_ULE: return ICMP_UGE;
2277 }
2278}
2279
Reid Spencer266e42b2006-12-23 06:05:41 +00002280ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2281 switch (pred) {
2282 default: assert(! "Unknown icmp predicate!");
2283 case ICMP_EQ: case ICMP_NE:
2284 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2285 return pred;
2286 case ICMP_UGT: return ICMP_SGT;
2287 case ICMP_ULT: return ICMP_SLT;
2288 case ICMP_UGE: return ICMP_SGE;
2289 case ICMP_ULE: return ICMP_SLE;
2290 }
2291}
2292
2293bool ICmpInst::isSignedPredicate(Predicate pred) {
2294 switch (pred) {
2295 default: assert(! "Unknown icmp predicate!");
2296 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2297 return true;
2298 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2299 case ICMP_UGE: case ICMP_ULE:
2300 return false;
2301 }
2302}
2303
Reid Spencer0286bc12007-02-28 22:00:54 +00002304/// Initialize a set of values that all satisfy the condition with C.
2305///
2306ConstantRange
2307ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2308 APInt Lower(C);
2309 APInt Upper(C);
2310 uint32_t BitWidth = C.getBitWidth();
2311 switch (pred) {
2312 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2313 case ICmpInst::ICMP_EQ: Upper++; break;
2314 case ICmpInst::ICMP_NE: Lower++; break;
2315 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2316 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2317 case ICmpInst::ICMP_UGT:
2318 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2319 break;
2320 case ICmpInst::ICMP_SGT:
2321 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2322 break;
2323 case ICmpInst::ICMP_ULE:
2324 Lower = APInt::getMinValue(BitWidth); Upper++;
2325 break;
2326 case ICmpInst::ICMP_SLE:
2327 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2328 break;
2329 case ICmpInst::ICMP_UGE:
2330 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2331 break;
2332 case ICmpInst::ICMP_SGE:
2333 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2334 break;
2335 }
2336 return ConstantRange(Lower, Upper);
2337}
2338
Reid Spencerd9436b62006-11-20 01:22:35 +00002339FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2340 switch (pred) {
2341 default:
2342 assert(!"Unknown icmp predicate!");
2343 case FCMP_OEQ: return FCMP_UNE;
2344 case FCMP_ONE: return FCMP_UEQ;
2345 case FCMP_OGT: return FCMP_ULE;
2346 case FCMP_OLT: return FCMP_UGE;
2347 case FCMP_OGE: return FCMP_ULT;
2348 case FCMP_OLE: return FCMP_UGT;
2349 case FCMP_UEQ: return FCMP_ONE;
2350 case FCMP_UNE: return FCMP_OEQ;
2351 case FCMP_UGT: return FCMP_OLE;
2352 case FCMP_ULT: return FCMP_OGE;
2353 case FCMP_UGE: return FCMP_OLT;
2354 case FCMP_ULE: return FCMP_OGT;
2355 case FCMP_ORD: return FCMP_UNO;
2356 case FCMP_UNO: return FCMP_ORD;
2357 case FCMP_TRUE: return FCMP_FALSE;
2358 case FCMP_FALSE: return FCMP_TRUE;
2359 }
2360}
2361
2362FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2363 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002364 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002365 case FCMP_FALSE: case FCMP_TRUE:
2366 case FCMP_OEQ: case FCMP_ONE:
2367 case FCMP_UEQ: case FCMP_UNE:
2368 case FCMP_ORD: case FCMP_UNO:
2369 return pred;
2370 case FCMP_OGT: return FCMP_OLT;
2371 case FCMP_OLT: return FCMP_OGT;
2372 case FCMP_OGE: return FCMP_OLE;
2373 case FCMP_OLE: return FCMP_OGE;
2374 case FCMP_UGT: return FCMP_ULT;
2375 case FCMP_ULT: return FCMP_UGT;
2376 case FCMP_UGE: return FCMP_ULE;
2377 case FCMP_ULE: return FCMP_UGE;
2378 }
2379}
2380
Reid Spencer266e42b2006-12-23 06:05:41 +00002381bool CmpInst::isUnsigned(unsigned short predicate) {
2382 switch (predicate) {
2383 default: return false;
2384 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2385 case ICmpInst::ICMP_UGE: return true;
2386 }
2387}
2388
2389bool CmpInst::isSigned(unsigned short predicate){
2390 switch (predicate) {
2391 default: return false;
2392 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2393 case ICmpInst::ICMP_SGE: return true;
2394 }
2395}
2396
2397bool CmpInst::isOrdered(unsigned short predicate) {
2398 switch (predicate) {
2399 default: return false;
2400 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2401 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2402 case FCmpInst::FCMP_ORD: return true;
2403 }
2404}
2405
2406bool CmpInst::isUnordered(unsigned short predicate) {
2407 switch (predicate) {
2408 default: return false;
2409 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2410 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2411 case FCmpInst::FCMP_UNO: return true;
2412 }
2413}
2414
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002415//===----------------------------------------------------------------------===//
2416// SwitchInst Implementation
2417//===----------------------------------------------------------------------===//
2418
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002419void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002420 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002421 ReservedSpace = 2+NumCases*2;
2422 NumOperands = 2;
2423 OperandList = new Use[ReservedSpace];
2424
2425 OperandList[0].init(Value, this);
2426 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002427}
2428
Chris Lattner2195fc42007-02-24 00:55:48 +00002429/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2430/// switch on and a default destination. The number of additional cases can
2431/// be specified here to make memory allocation more efficient. This
2432/// constructor can also autoinsert before another instruction.
2433SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2434 Instruction *InsertBefore)
2435 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2436 init(Value, Default, NumCases);
2437}
2438
2439/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2440/// switch on and a default destination. The number of additional cases can
2441/// be specified here to make memory allocation more efficient. This
2442/// constructor also autoinserts at the end of the specified BasicBlock.
2443SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2444 BasicBlock *InsertAtEnd)
2445 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2446 init(Value, Default, NumCases);
2447}
2448
Misha Brukmanb1c93172005-04-21 23:48:37 +00002449SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002450 : TerminatorInst(Type::VoidTy, Instruction::Switch,
2451 new Use[SI.getNumOperands()], SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002452 Use *OL = OperandList, *InOL = SI.OperandList;
2453 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2454 OL[i].init(InOL[i], this);
2455 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002456 }
2457}
2458
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002459SwitchInst::~SwitchInst() {
2460 delete [] OperandList;
2461}
2462
2463
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002464/// addCase - Add an entry to the switch instruction...
2465///
Chris Lattner47ac1872005-02-24 05:32:09 +00002466void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002467 unsigned OpNo = NumOperands;
2468 if (OpNo+2 > ReservedSpace)
2469 resizeOperands(0); // Get more space!
2470 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002471 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002472 NumOperands = OpNo+2;
2473 OperandList[OpNo].init(OnVal, this);
2474 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002475}
2476
2477/// removeCase - This method removes the specified successor from the switch
2478/// instruction. Note that this cannot be used to remove the default
2479/// destination (successor #0).
2480///
2481void SwitchInst::removeCase(unsigned idx) {
2482 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002483 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2484
2485 unsigned NumOps = getNumOperands();
2486 Use *OL = OperandList;
2487
2488 // Move everything after this operand down.
2489 //
2490 // FIXME: we could just swap with the end of the list, then erase. However,
2491 // client might not expect this to happen. The code as it is thrashes the
2492 // use/def lists, which is kinda lame.
2493 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2494 OL[i-2] = OL[i];
2495 OL[i-2+1] = OL[i+1];
2496 }
2497
2498 // Nuke the last value.
2499 OL[NumOps-2].set(0);
2500 OL[NumOps-2+1].set(0);
2501 NumOperands = NumOps-2;
2502}
2503
2504/// resizeOperands - resize operands - This adjusts the length of the operands
2505/// list according to the following behavior:
2506/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2507/// of operation. This grows the number of ops by 1.5 times.
2508/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2509/// 3. If NumOps == NumOperands, trim the reserved space.
2510///
2511void SwitchInst::resizeOperands(unsigned NumOps) {
2512 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002513 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002514 } else if (NumOps*2 > NumOperands) {
2515 // No resize needed.
2516 if (ReservedSpace >= NumOps) return;
2517 } else if (NumOps == NumOperands) {
2518 if (ReservedSpace == NumOps) return;
2519 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002520 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002521 }
2522
2523 ReservedSpace = NumOps;
2524 Use *NewOps = new Use[NumOps];
2525 Use *OldOps = OperandList;
2526 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2527 NewOps[i].init(OldOps[i], this);
2528 OldOps[i].set(0);
2529 }
2530 delete [] OldOps;
2531 OperandList = NewOps;
2532}
2533
2534
2535BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2536 return getSuccessor(idx);
2537}
2538unsigned SwitchInst::getNumSuccessorsV() const {
2539 return getNumSuccessors();
2540}
2541void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2542 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002543}
Chris Lattnerf22be932004-10-15 23:52:53 +00002544
2545
2546// Define these methods here so vtables don't get emitted into every translation
2547// unit that uses these classes.
2548
2549GetElementPtrInst *GetElementPtrInst::clone() const {
2550 return new GetElementPtrInst(*this);
2551}
2552
2553BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002554 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002555}
2556
Chris Lattner0b490b02007-08-24 20:48:18 +00002557FCmpInst* FCmpInst::clone() const {
2558 return new FCmpInst(getPredicate(), Ops[0], Ops[1]);
2559}
2560ICmpInst* ICmpInst::clone() const {
2561 return new ICmpInst(getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002562}
2563
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002564MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2565AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2566FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2567LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2568StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2569CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2570CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2571CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2572CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2573CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2574CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2575CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2576CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2577CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2578CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2579CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2580CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2581CallInst *CallInst::clone() const { return new CallInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002582SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2583VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2584
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002585ExtractElementInst *ExtractElementInst::clone() const {
2586 return new ExtractElementInst(*this);
2587}
2588InsertElementInst *InsertElementInst::clone() const {
2589 return new InsertElementInst(*this);
2590}
2591ShuffleVectorInst *ShuffleVectorInst::clone() const {
2592 return new ShuffleVectorInst(*this);
2593}
Chris Lattnerf22be932004-10-15 23:52:53 +00002594PHINode *PHINode::clone() const { return new PHINode(*this); }
2595ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2596BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2597SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2598InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2599UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002600UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}