blob: 5207ea52f49325370b0bc340a1d82d9cb6fbf2f6 [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}
Duncan Sands38ef3a82007-12-03 20:06:50 +000056bool CallSite::doesNotAccessMemory() const {
57 if (CallInst *CI = dyn_cast<CallInst>(I))
58 return CI->doesNotAccessMemory();
59 else
60 return cast<InvokeInst>(I)->doesNotAccessMemory();
61}
62bool CallSite::onlyReadsMemory() const {
63 if (CallInst *CI = dyn_cast<CallInst>(I))
64 return CI->onlyReadsMemory();
65 else
66 return cast<InvokeInst>(I)->onlyReadsMemory();
67}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000068
Chris Lattner1c12a882006-06-21 16:53:47 +000069
Gordon Henriksen14a55692007-12-10 02:14:30 +000070
71//===----------------------------------------------------------------------===//
72// TerminatorInst Class
73//===----------------------------------------------------------------------===//
74
75// Out of line virtual method, so the vtable, etc has a home.
76TerminatorInst::~TerminatorInst() {
77}
78
79// Out of line virtual method, so the vtable, etc has a home.
80UnaryInstruction::~UnaryInstruction() {
81}
82
83
Chris Lattnerafdb3de2005-01-29 00:35:16 +000084//===----------------------------------------------------------------------===//
85// PHINode Class
86//===----------------------------------------------------------------------===//
87
88PHINode::PHINode(const PHINode &PN)
89 : Instruction(PN.getType(), Instruction::PHI,
90 new Use[PN.getNumOperands()], PN.getNumOperands()),
91 ReservedSpace(PN.getNumOperands()) {
92 Use *OL = OperandList;
93 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
94 OL[i].init(PN.getOperand(i), this);
95 OL[i+1].init(PN.getOperand(i+1), this);
96 }
97}
98
Gordon Henriksen14a55692007-12-10 02:14:30 +000099PHINode::~PHINode() {
100 delete [] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000101}
102
103// removeIncomingValue - Remove an incoming value. This is useful if a
104// predecessor basic block is deleted.
105Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
106 unsigned NumOps = getNumOperands();
107 Use *OL = OperandList;
108 assert(Idx*2 < NumOps && "BB not in PHI node!");
109 Value *Removed = OL[Idx*2];
110
111 // Move everything after this operand down.
112 //
113 // FIXME: we could just swap with the end of the list, then erase. However,
114 // client might not expect this to happen. The code as it is thrashes the
115 // use/def lists, which is kinda lame.
116 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
117 OL[i-2] = OL[i];
118 OL[i-2+1] = OL[i+1];
119 }
120
121 // Nuke the last value.
122 OL[NumOps-2].set(0);
123 OL[NumOps-2+1].set(0);
124 NumOperands = NumOps-2;
125
126 // If the PHI node is dead, because it has zero entries, nuke it now.
127 if (NumOps == 2 && DeletePHIIfEmpty) {
128 // If anyone is using this PHI, make them use a dummy value instead...
129 replaceAllUsesWith(UndefValue::get(getType()));
130 eraseFromParent();
131 }
132 return Removed;
133}
134
135/// resizeOperands - resize operands - This adjusts the length of the operands
136/// list according to the following behavior:
137/// 1. If NumOps == 0, grow the operand list in response to a push_back style
138/// of operation. This grows the number of ops by 1.5 times.
139/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
140/// 3. If NumOps == NumOperands, trim the reserved space.
141///
142void PHINode::resizeOperands(unsigned NumOps) {
143 if (NumOps == 0) {
144 NumOps = (getNumOperands())*3/2;
145 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
146 } else if (NumOps*2 > NumOperands) {
147 // No resize needed.
148 if (ReservedSpace >= NumOps) return;
149 } else if (NumOps == NumOperands) {
150 if (ReservedSpace == NumOps) return;
151 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000152 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000153 }
154
155 ReservedSpace = NumOps;
156 Use *NewOps = new Use[NumOps];
157 Use *OldOps = OperandList;
158 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
159 NewOps[i].init(OldOps[i], this);
160 OldOps[i].set(0);
161 }
162 delete [] OldOps;
163 OperandList = NewOps;
164}
165
Nate Begemanb3923212005-08-04 23:24:19 +0000166/// hasConstantValue - If the specified PHI node always merges together the same
167/// value, return the value, otherwise return null.
168///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000169Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000170 // If the PHI node only has one incoming value, eliminate the PHI node...
171 if (getNumIncomingValues() == 1)
Chris Lattner6e709c12005-08-05 15:37:31 +0000172 if (getIncomingValue(0) != this) // not X = phi X
173 return getIncomingValue(0);
174 else
175 return UndefValue::get(getType()); // Self cycle is dead.
176
Nate Begemanb3923212005-08-04 23:24:19 +0000177 // Otherwise if all of the incoming values are the same for the PHI, replace
178 // the PHI node with the incoming value.
179 //
180 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000181 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000182 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000183 if (isa<UndefValue>(getIncomingValue(i)))
184 HasUndefInput = true;
185 else if (getIncomingValue(i) != this) // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000186 if (InVal && getIncomingValue(i) != InVal)
187 return 0; // Not the same, bail out.
188 else
189 InVal = getIncomingValue(i);
190
191 // The only case that could cause InVal to be null is if we have a PHI node
192 // that only has entries for itself. In this case, there is no entry into the
193 // loop, so kill the PHI.
194 //
195 if (InVal == 0) InVal = UndefValue::get(getType());
196
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000197 // If we have a PHI node like phi(X, undef, X), where X is defined by some
198 // instruction, we cannot always return X as the result of the PHI node. Only
199 // do this if X is not an instruction (thus it must dominate the PHI block),
200 // or if the client is prepared to deal with this possibility.
201 if (HasUndefInput && !AllowNonDominatingInstruction)
202 if (Instruction *IV = dyn_cast<Instruction>(InVal))
203 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000204 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000205 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000206 return 0; // Cannot guarantee that InVal dominates this PHINode.
207
Nate Begemanb3923212005-08-04 23:24:19 +0000208 // All of the incoming values are the same, return the value now.
209 return InVal;
210}
211
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000212
213//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000214// CallInst Implementation
215//===----------------------------------------------------------------------===//
216
Gordon Henriksen14a55692007-12-10 02:14:30 +0000217CallInst::~CallInst() {
218 delete [] OperandList;
219 if (ParamAttrs)
220 ParamAttrs->dropRef();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000221}
222
Chris Lattner054ba2c2007-02-13 00:58:44 +0000223void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Reid Spencer019c8862007-04-09 15:01:12 +0000224 ParamAttrs = 0;
Chris Lattner054ba2c2007-02-13 00:58:44 +0000225 NumOperands = NumParams+1;
226 Use *OL = OperandList = new Use[NumParams+1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000227 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000228
Misha Brukmanb1c93172005-04-21 23:48:37 +0000229 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000230 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000231 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000232
Chris Lattner054ba2c2007-02-13 00:58:44 +0000233 assert((NumParams == FTy->getNumParams() ||
234 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000235 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000236 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000237 assert((i >= FTy->getNumParams() ||
238 FTy->getParamType(i) == Params[i]->getType()) &&
239 "Calling a function with a bad signature!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000240 OL[i+1].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000241 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000242}
243
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000244void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Reid Spencer019c8862007-04-09 15:01:12 +0000245 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000246 NumOperands = 3;
247 Use *OL = OperandList = new Use[3];
248 OL[0].init(Func, this);
249 OL[1].init(Actual1, this);
250 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000251
252 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000253 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000254 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000255
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000256 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000257 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000258 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000259 assert((0 >= FTy->getNumParams() ||
260 FTy->getParamType(0) == Actual1->getType()) &&
261 "Calling a function with a bad signature!");
262 assert((1 >= FTy->getNumParams() ||
263 FTy->getParamType(1) == Actual2->getType()) &&
264 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000265}
266
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000267void CallInst::init(Value *Func, Value *Actual) {
Reid Spencer019c8862007-04-09 15:01:12 +0000268 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000269 NumOperands = 2;
270 Use *OL = OperandList = new Use[2];
271 OL[0].init(Func, this);
272 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000273
274 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000275 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000276 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000277
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000278 assert((FTy->getNumParams() == 1 ||
279 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000280 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000281 assert((0 == FTy->getNumParams() ||
282 FTy->getParamType(0) == Actual->getType()) &&
283 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000284}
285
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000286void CallInst::init(Value *Func) {
Reid Spencer019c8862007-04-09 15:01:12 +0000287 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000288 NumOperands = 1;
289 Use *OL = OperandList = new Use[1];
290 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000291
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000292 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000293 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000294 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000295
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000296 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000297}
298
David Greene17a5dfe2007-08-01 03:43:44 +0000299#if 0
300// Leave for llvm-gcc
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000301CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000302 const std::string &Name, BasicBlock *InsertAtEnd)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000303 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
David Greene17a5dfe2007-08-01 03:43:44 +0000304 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000305 Instruction::Call, 0, 0, InsertAtEnd) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000306 init(Func, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000307 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000308}
309CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
310 const std::string &Name, Instruction *InsertBefore)
David Greene17a5dfe2007-08-01 03:43:44 +0000311 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
312 ->getElementType())->getReturnType(),
313 Instruction::Call, 0, 0, InsertBefore) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000314 init(Func, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000315 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000316}
317
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000318CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
319 const std::string &Name, Instruction *InsertBefore)
320 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
321 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000322 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000323 init(Func, Actual1, Actual2);
Chris Lattner2195fc42007-02-24 00:55:48 +0000324 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000325}
326
327CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
328 const std::string &Name, BasicBlock *InsertAtEnd)
329 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
330 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000331 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000332 init(Func, Actual1, Actual2);
Chris Lattner2195fc42007-02-24 00:55:48 +0000333 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000334}
David Greene17a5dfe2007-08-01 03:43:44 +0000335#endif
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000336CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000337 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000338 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
339 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000340 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000341 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000342 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000343}
344
345CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
346 BasicBlock *InsertAtEnd)
347 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
348 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000349 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000350 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000351 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000352}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000353CallInst::CallInst(Value *Func, const std::string &Name,
354 Instruction *InsertBefore)
355 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
356 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000357 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000358 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000359 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000360}
361
362CallInst::CallInst(Value *Func, const std::string &Name,
363 BasicBlock *InsertAtEnd)
364 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
365 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000366 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000367 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000368 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000369}
370
Misha Brukmanb1c93172005-04-21 23:48:37 +0000371CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000372 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000373 CI.getNumOperands()),
374 ParamAttrs(0) {
375 setParamAttrs(CI.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000376 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000377 Use *OL = OperandList;
378 Use *InOL = CI.OperandList;
379 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
380 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000381}
382
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000383void CallInst::setParamAttrs(const ParamAttrsList *newAttrs) {
384 if (ParamAttrs == newAttrs)
385 return;
386
Reid Spencerc6a83842007-04-22 17:28:03 +0000387 if (ParamAttrs)
388 ParamAttrs->dropRef();
389
390 if (newAttrs)
391 newAttrs->addRef();
392
393 ParamAttrs = newAttrs;
394}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000395
Duncan Sands5208d1a2007-11-28 17:07:01 +0000396bool CallInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
397 if (ParamAttrs && ParamAttrs->paramHasAttr(i, attr))
398 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000399 if (const Function *F = getCalledFunction())
400 return F->paramHasAttr(i, attr);
401 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000402}
403
Duncan Sands5208d1a2007-11-28 17:07:01 +0000404
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000405//===----------------------------------------------------------------------===//
406// InvokeInst Implementation
407//===----------------------------------------------------------------------===//
408
Gordon Henriksen14a55692007-12-10 02:14:30 +0000409InvokeInst::~InvokeInst() {
410 delete [] OperandList;
411 if (ParamAttrs)
412 ParamAttrs->dropRef();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000413}
414
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000415void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000416 Value* const *Args, unsigned NumArgs) {
Reid Spencerce38beb2007-04-09 18:00:57 +0000417 ParamAttrs = 0;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000418 NumOperands = 3+NumArgs;
419 Use *OL = OperandList = new Use[3+NumArgs];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000420 OL[0].init(Fn, this);
421 OL[1].init(IfNormal, this);
422 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000423 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000424 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000425 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000426
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000427 assert((NumArgs == FTy->getNumParams()) ||
428 (FTy->isVarArg() && NumArgs > FTy->getNumParams()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000429 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000430
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000431 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000432 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000433 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000434 "Invoking a function with a bad signature!");
435
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000436 OL[i+3].init(Args[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000437 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000438}
439
Misha Brukmanb1c93172005-04-21 23:48:37 +0000440InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000441 : TerminatorInst(II.getType(), Instruction::Invoke,
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000442 new Use[II.getNumOperands()], II.getNumOperands()),
443 ParamAttrs(0) {
444 setParamAttrs(II.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000445 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000446 Use *OL = OperandList, *InOL = II.OperandList;
447 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
448 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000449}
450
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000451BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
452 return getSuccessor(idx);
453}
454unsigned InvokeInst::getNumSuccessorsV() const {
455 return getNumSuccessors();
456}
457void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
458 return setSuccessor(idx, B);
459}
460
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000461void InvokeInst::setParamAttrs(const ParamAttrsList *newAttrs) {
462 if (ParamAttrs == newAttrs)
463 return;
464
Reid Spencerc6a83842007-04-22 17:28:03 +0000465 if (ParamAttrs)
466 ParamAttrs->dropRef();
467
468 if (newAttrs)
469 newAttrs->addRef();
470
471 ParamAttrs = newAttrs;
472}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000473
Duncan Sands5208d1a2007-11-28 17:07:01 +0000474bool InvokeInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
475 if (ParamAttrs && ParamAttrs->paramHasAttr(i, attr))
476 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000477 if (const Function *F = getCalledFunction())
478 return F->paramHasAttr(i, attr);
479 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000480}
481
Duncan Sands5208d1a2007-11-28 17:07:01 +0000482
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000483//===----------------------------------------------------------------------===//
484// ReturnInst Implementation
485//===----------------------------------------------------------------------===//
486
Chris Lattner2195fc42007-02-24 00:55:48 +0000487ReturnInst::ReturnInst(const ReturnInst &RI)
488 : TerminatorInst(Type::VoidTy, Instruction::Ret,
489 &RetVal, RI.getNumOperands()) {
490 if (RI.getNumOperands())
491 RetVal.init(RI.RetVal, this);
492}
493
494ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
495 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) {
496 init(retVal);
497}
498ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
499 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
500 init(retVal);
501}
502ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
503 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
504}
505
506
507
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000508void ReturnInst::init(Value *retVal) {
509 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000510 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000511 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000512 NumOperands = 1;
513 RetVal.init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000514 }
515}
516
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000517unsigned ReturnInst::getNumSuccessorsV() const {
518 return getNumSuccessors();
519}
520
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000521// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
522// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000523void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000524 assert(0 && "ReturnInst has no successors!");
525}
526
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000527BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
528 assert(0 && "ReturnInst has no successors!");
529 abort();
530 return 0;
531}
532
533
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000534//===----------------------------------------------------------------------===//
535// UnwindInst Implementation
536//===----------------------------------------------------------------------===//
537
Chris Lattner2195fc42007-02-24 00:55:48 +0000538UnwindInst::UnwindInst(Instruction *InsertBefore)
539 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
540}
541UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
542 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
543}
544
545
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000546unsigned UnwindInst::getNumSuccessorsV() const {
547 return getNumSuccessors();
548}
549
550void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000551 assert(0 && "UnwindInst has no successors!");
552}
553
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000554BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
555 assert(0 && "UnwindInst has no successors!");
556 abort();
557 return 0;
558}
559
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000560//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000561// UnreachableInst Implementation
562//===----------------------------------------------------------------------===//
563
Chris Lattner2195fc42007-02-24 00:55:48 +0000564UnreachableInst::UnreachableInst(Instruction *InsertBefore)
565 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
566}
567UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
568 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
569}
570
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000571unsigned UnreachableInst::getNumSuccessorsV() const {
572 return getNumSuccessors();
573}
574
575void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
576 assert(0 && "UnwindInst has no successors!");
577}
578
579BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
580 assert(0 && "UnwindInst has no successors!");
581 abort();
582 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000583}
584
585//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000586// BranchInst Implementation
587//===----------------------------------------------------------------------===//
588
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000589void BranchInst::AssertOK() {
590 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000591 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000592 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000593}
594
Chris Lattner2195fc42007-02-24 00:55:48 +0000595BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
596 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) {
597 assert(IfTrue != 0 && "Branch destination may not be null!");
598 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
599}
600BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
601 Instruction *InsertBefore)
602: TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) {
603 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
604 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
605 Ops[2].init(Cond, this);
606#ifndef NDEBUG
607 AssertOK();
608#endif
609}
610
611BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
612 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) {
613 assert(IfTrue != 0 && "Branch destination may not be null!");
614 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
615}
616
617BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
618 BasicBlock *InsertAtEnd)
619 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) {
620 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
621 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
622 Ops[2].init(Cond, this);
623#ifndef NDEBUG
624 AssertOK();
625#endif
626}
627
628
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000629BranchInst::BranchInst(const BranchInst &BI) :
Chris Lattner2195fc42007-02-24 00:55:48 +0000630 TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000631 OperandList[0].init(BI.getOperand(0), this);
632 if (BI.getNumOperands() != 1) {
633 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
634 OperandList[1].init(BI.getOperand(1), this);
635 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000636 }
637}
638
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000639BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
640 return getSuccessor(idx);
641}
642unsigned BranchInst::getNumSuccessorsV() const {
643 return getNumSuccessors();
644}
645void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
646 setSuccessor(idx, B);
647}
648
649
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000650//===----------------------------------------------------------------------===//
651// AllocationInst Implementation
652//===----------------------------------------------------------------------===//
653
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000654static Value *getAISize(Value *Amt) {
655 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000656 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000657 else {
658 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000659 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000660 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000661 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000662 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000663 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000664}
665
Misha Brukmanb1c93172005-04-21 23:48:37 +0000666AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000667 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000668 Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000669 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000670 InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000671 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000672 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000673 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000674}
675
Misha Brukmanb1c93172005-04-21 23:48:37 +0000676AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000677 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000678 BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000679 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000680 InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000681 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000682 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000683 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000684}
685
Gordon Henriksen14a55692007-12-10 02:14:30 +0000686// Out of line virtual method, so the vtable, etc has a home.
687AllocationInst::~AllocationInst() {
688}
689
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000690bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000691 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
692 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000693 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000694}
695
696const Type *AllocationInst::getAllocatedType() const {
697 return getType()->getElementType();
698}
699
700AllocaInst::AllocaInst(const AllocaInst &AI)
701 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000702 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000703}
704
705MallocInst::MallocInst(const MallocInst &MI)
706 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000707 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000708}
709
710//===----------------------------------------------------------------------===//
711// FreeInst Implementation
712//===----------------------------------------------------------------------===//
713
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000714void FreeInst::AssertOK() {
715 assert(isa<PointerType>(getOperand(0)->getType()) &&
716 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000717}
718
719FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000720 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000721 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000722}
723
724FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000725 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000726 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000727}
728
729
730//===----------------------------------------------------------------------===//
731// LoadInst Implementation
732//===----------------------------------------------------------------------===//
733
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000734void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000735 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000736 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000737}
738
739LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000740 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000741 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000742 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000743 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000744 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000745 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000746}
747
748LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000749 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000750 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000751 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000752 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000753 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000754 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000755}
756
757LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
758 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000759 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000760 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000761 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000762 setAlignment(0);
763 AssertOK();
764 setName(Name);
765}
766
767LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
768 unsigned Align, Instruction *InsertBef)
769 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
770 Load, Ptr, InsertBef) {
771 setVolatile(isVolatile);
772 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000773 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000774 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000775}
776
Dan Gohman68659282007-07-18 20:51:11 +0000777LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
778 unsigned Align, BasicBlock *InsertAE)
779 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
780 Load, Ptr, InsertAE) {
781 setVolatile(isVolatile);
782 setAlignment(Align);
783 AssertOK();
784 setName(Name);
785}
786
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000787LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
788 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000789 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000790 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000791 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000792 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000793 AssertOK();
794 setName(Name);
795}
796
797
798
799LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000800 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
801 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000802 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000803 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000804 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000805 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000806}
807
808LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000809 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
810 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000811 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000812 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000813 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000814 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000815}
816
817LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
818 Instruction *InsertBef)
819: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000820 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000821 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000822 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000823 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000824 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000825}
826
827LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
828 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000829 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
830 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000831 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000832 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000833 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000834 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000835}
836
Christopher Lamb84485702007-04-22 19:24:39 +0000837void LoadInst::setAlignment(unsigned Align) {
838 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
839 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
840}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000841
842//===----------------------------------------------------------------------===//
843// StoreInst Implementation
844//===----------------------------------------------------------------------===//
845
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000846void StoreInst::AssertOK() {
847 assert(isa<PointerType>(getOperand(1)->getType()) &&
848 "Ptr must have pointer type!");
849 assert(getOperand(0)->getType() ==
850 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000851 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000852}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000853
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000854
855StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000856 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000857 Ops[0].init(val, this);
858 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000859 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000860 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000861 AssertOK();
862}
863
864StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000865 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000866 Ops[0].init(val, this);
867 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000868 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000869 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000870 AssertOK();
871}
872
Misha Brukmanb1c93172005-04-21 23:48:37 +0000873StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000874 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000875 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000876 Ops[0].init(val, this);
877 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000878 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000879 setAlignment(0);
880 AssertOK();
881}
882
883StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
884 unsigned Align, Instruction *InsertBefore)
885 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
886 Ops[0].init(val, this);
887 Ops[1].init(addr, this);
888 setVolatile(isVolatile);
889 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000890 AssertOK();
891}
892
Misha Brukmanb1c93172005-04-21 23:48:37 +0000893StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000894 unsigned Align, BasicBlock *InsertAtEnd)
895 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
896 Ops[0].init(val, this);
897 Ops[1].init(addr, this);
898 setVolatile(isVolatile);
899 setAlignment(Align);
900 AssertOK();
901}
902
903StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000904 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000905 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000906 Ops[0].init(val, this);
907 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000908 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000909 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000910 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000911}
912
Christopher Lamb84485702007-04-22 19:24:39 +0000913void StoreInst::setAlignment(unsigned Align) {
914 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
915 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
916}
917
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000918//===----------------------------------------------------------------------===//
919// GetElementPtrInst Implementation
920//===----------------------------------------------------------------------===//
921
Chris Lattner79807c3d2007-01-31 19:47:18 +0000922void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
923 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000924 Use *OL = OperandList = new Use[NumOperands];
925 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000926
Chris Lattner79807c3d2007-01-31 19:47:18 +0000927 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000928 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000929}
930
Chris Lattner82981202005-05-03 05:43:30 +0000931void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
932 NumOperands = 2;
933 Use *OL = OperandList = new Use[2];
934 OL[0].init(Ptr, this);
935 OL[1].init(Idx, this);
936}
937
Chris Lattner82981202005-05-03 05:43:30 +0000938GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
939 const std::string &Name, Instruction *InBe)
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000940 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
941 cast<PointerType>(Ptr->getType())->getAddressSpace()),
Chris Lattner2195fc42007-02-24 00:55:48 +0000942 GetElementPtr, 0, 0, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +0000943 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000944 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000945}
946
947GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
948 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000949 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
950 cast<PointerType>(Ptr->getType())->getAddressSpace()),
Chris Lattner2195fc42007-02-24 00:55:48 +0000951 GetElementPtr, 0, 0, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +0000952 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000953 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000954}
955
Gordon Henriksen14a55692007-12-10 02:14:30 +0000956GetElementPtrInst::~GetElementPtrInst() {
957 delete[] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000958}
959
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000960// getIndexedType - Returns the type of the element that would be loaded with
961// a load instruction with the specified parameters.
962//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000963// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000964// pointer type.
965//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000966const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +0000967 Value* const *Idxs,
968 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000969 bool AllowCompositeLeaf) {
970 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
971
972 // Handle the special case of the empty set index set...
Chris Lattner302116a2007-01-31 04:40:28 +0000973 if (NumIdx == 0)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000974 if (AllowCompositeLeaf ||
975 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
976 return cast<PointerType>(Ptr)->getElementType();
977 else
978 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000979
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000980 unsigned CurIdx = 0;
981 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +0000982 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000983 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
984 return 0; // Can't load a whole structure or array!?!?
985 }
986
Chris Lattner302116a2007-01-31 04:40:28 +0000987 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000988 if (isa<PointerType>(CT) && CurIdx != 1)
989 return 0; // Can only index into pointer types at the first index!
990 if (!CT->indexValid(Index)) return 0;
991 Ptr = CT->getTypeAtIndex(Index);
992
993 // If the new type forwards to another type, then it is in the middle
994 // of being refined to another type (and hence, may have dropped all
995 // references to what it was using before). So, use the new forwarded
996 // type.
997 if (const Type * Ty = Ptr->getForwardedType()) {
998 Ptr = Ty;
999 }
1000 }
Chris Lattner302116a2007-01-31 04:40:28 +00001001 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001002}
1003
Chris Lattner82981202005-05-03 05:43:30 +00001004const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1005 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1006 if (!PTy) return 0; // Type isn't a pointer type!
1007
1008 // Check the pointer index.
1009 if (!PTy->indexValid(Idx)) return 0;
1010
Chris Lattnerc2233332005-05-03 16:44:45 +00001011 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001012}
1013
Chris Lattner45f15572007-04-14 00:12:57 +00001014
1015/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1016/// zeros. If so, the result pointer and the first operand have the same
1017/// value, just potentially different types.
1018bool GetElementPtrInst::hasAllZeroIndices() const {
1019 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1020 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1021 if (!CI->isZero()) return false;
1022 } else {
1023 return false;
1024 }
1025 }
1026 return true;
1027}
1028
Chris Lattner27058292007-04-27 20:35:56 +00001029/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1030/// constant integers. If so, the result pointer and the first operand have
1031/// a constant offset between them.
1032bool GetElementPtrInst::hasAllConstantIndices() const {
1033 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1034 if (!isa<ConstantInt>(getOperand(i)))
1035 return false;
1036 }
1037 return true;
1038}
1039
Chris Lattner45f15572007-04-14 00:12:57 +00001040
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001041//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001042// ExtractElementInst Implementation
1043//===----------------------------------------------------------------------===//
1044
1045ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001046 const std::string &Name,
1047 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001048 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001049 ExtractElement, Ops, 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001050 assert(isValidOperands(Val, Index) &&
1051 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +00001052 Ops[0].init(Val, this);
1053 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001054 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001055}
1056
Chris Lattner65511ff2006-10-05 06:24:58 +00001057ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1058 const std::string &Name,
1059 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001060 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001061 ExtractElement, Ops, 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001062 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001063 assert(isValidOperands(Val, Index) &&
1064 "Invalid extractelement instruction operands!");
1065 Ops[0].init(Val, this);
1066 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001067 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001068}
1069
1070
Robert Bocchino23004482006-01-10 19:05:34 +00001071ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001072 const std::string &Name,
1073 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001074 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001075 ExtractElement, Ops, 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001076 assert(isValidOperands(Val, Index) &&
1077 "Invalid extractelement instruction operands!");
1078
Robert Bocchino23004482006-01-10 19:05:34 +00001079 Ops[0].init(Val, this);
1080 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001081 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001082}
1083
Chris Lattner65511ff2006-10-05 06:24:58 +00001084ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1085 const std::string &Name,
1086 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001087 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001088 ExtractElement, Ops, 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001089 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001090 assert(isValidOperands(Val, Index) &&
1091 "Invalid extractelement instruction operands!");
1092
1093 Ops[0].init(Val, this);
1094 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001095 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001096}
1097
1098
Chris Lattner54865b32006-04-08 04:05:48 +00001099bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001100 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001101 return false;
1102 return true;
1103}
1104
1105
Robert Bocchino23004482006-01-10 19:05:34 +00001106//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001107// InsertElementInst Implementation
1108//===----------------------------------------------------------------------===//
1109
Chris Lattner0875d942006-04-14 22:20:32 +00001110InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1111 : Instruction(IE.getType(), InsertElement, Ops, 3) {
1112 Ops[0].init(IE.Ops[0], this);
1113 Ops[1].init(IE.Ops[1], this);
1114 Ops[2].init(IE.Ops[2], this);
1115}
Chris Lattner54865b32006-04-08 04:05:48 +00001116InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001117 const std::string &Name,
1118 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001119 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001120 assert(isValidOperands(Vec, Elt, Index) &&
1121 "Invalid insertelement instruction operands!");
1122 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001123 Ops[1].init(Elt, this);
1124 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001125 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001126}
1127
Chris Lattner65511ff2006-10-05 06:24:58 +00001128InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1129 const std::string &Name,
1130 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001131 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001132 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001133 assert(isValidOperands(Vec, Elt, Index) &&
1134 "Invalid insertelement instruction operands!");
1135 Ops[0].init(Vec, this);
1136 Ops[1].init(Elt, this);
1137 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001138 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001139}
1140
1141
Chris Lattner54865b32006-04-08 04:05:48 +00001142InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001143 const std::string &Name,
1144 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001145 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001146 assert(isValidOperands(Vec, Elt, Index) &&
1147 "Invalid insertelement instruction operands!");
1148
1149 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001150 Ops[1].init(Elt, this);
1151 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001152 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001153}
1154
Chris Lattner65511ff2006-10-05 06:24:58 +00001155InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1156 const std::string &Name,
1157 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001158: Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001159 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001160 assert(isValidOperands(Vec, Elt, Index) &&
1161 "Invalid insertelement instruction operands!");
1162
1163 Ops[0].init(Vec, this);
1164 Ops[1].init(Elt, this);
1165 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001166 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001167}
1168
Chris Lattner54865b32006-04-08 04:05:48 +00001169bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1170 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001171 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001172 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001173
Reid Spencerd84d35b2007-02-15 02:26:10 +00001174 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001175 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001176
Reid Spencer8d9336d2006-12-31 05:26:44 +00001177 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001178 return false; // Third operand of insertelement must be uint.
1179 return true;
1180}
1181
1182
Robert Bocchinoca27f032006-01-17 20:07:22 +00001183//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001184// ShuffleVectorInst Implementation
1185//===----------------------------------------------------------------------===//
1186
Chris Lattner0875d942006-04-14 22:20:32 +00001187ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1188 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
1189 Ops[0].init(SV.Ops[0], this);
1190 Ops[1].init(SV.Ops[1], this);
1191 Ops[2].init(SV.Ops[2], this);
1192}
1193
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001194ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1195 const std::string &Name,
1196 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00001197 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001198 assert(isValidOperands(V1, V2, Mask) &&
1199 "Invalid shuffle vector instruction operands!");
1200 Ops[0].init(V1, this);
1201 Ops[1].init(V2, this);
1202 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001203 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001204}
1205
1206ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1207 const std::string &Name,
1208 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00001209 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001210 assert(isValidOperands(V1, V2, Mask) &&
1211 "Invalid shuffle vector instruction operands!");
1212
1213 Ops[0].init(V1, this);
1214 Ops[1].init(V2, this);
1215 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001216 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001217}
1218
1219bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1220 const Value *Mask) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001221 if (!isa<VectorType>(V1->getType())) return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001222 if (V1->getType() != V2->getType()) return false;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001223 if (!isa<VectorType>(Mask->getType()) ||
1224 cast<VectorType>(Mask->getType())->getElementType() != Type::Int32Ty ||
1225 cast<VectorType>(Mask->getType())->getNumElements() !=
1226 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001227 return false;
1228 return true;
1229}
1230
1231
1232//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001233// BinaryOperator Class
1234//===----------------------------------------------------------------------===//
1235
Chris Lattner2195fc42007-02-24 00:55:48 +00001236BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1237 const Type *Ty, const std::string &Name,
1238 Instruction *InsertBefore)
1239 : Instruction(Ty, iType, Ops, 2, InsertBefore) {
1240 Ops[0].init(S1, this);
1241 Ops[1].init(S2, this);
1242 init(iType);
1243 setName(Name);
1244}
1245
1246BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1247 const Type *Ty, const std::string &Name,
1248 BasicBlock *InsertAtEnd)
1249 : Instruction(Ty, iType, Ops, 2, InsertAtEnd) {
1250 Ops[0].init(S1, this);
1251 Ops[1].init(S2, this);
1252 init(iType);
1253 setName(Name);
1254}
1255
1256
1257void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001258 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001259 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001260 assert(LHS->getType() == RHS->getType() &&
1261 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001262#ifndef NDEBUG
1263 switch (iType) {
1264 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001265 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001266 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001267 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001268 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001269 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001270 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001271 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001272 case UDiv:
1273 case SDiv:
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 Spencer7e80b0b2006-10-26 06:15:43 +00001278 "Incorrect operand type (not integer) for S/UDIV");
1279 break;
1280 case FDiv:
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 Spencer7e80b0b2006-10-26 06:15:43 +00001285 && "Incorrect operand type (not floating point) for FDIV");
1286 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001287 case URem:
1288 case SRem:
1289 assert(getType() == LHS->getType() &&
1290 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001291 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1292 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001293 "Incorrect operand type (not integer) for S/UREM");
1294 break;
1295 case FRem:
1296 assert(getType() == LHS->getType() &&
1297 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001298 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1299 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001300 && "Incorrect operand type (not floating point) for FREM");
1301 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001302 case Shl:
1303 case LShr:
1304 case AShr:
1305 assert(getType() == LHS->getType() &&
1306 "Shift operation should return same type as operands!");
1307 assert(getType()->isInteger() &&
1308 "Shift operation requires integer operands");
1309 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001310 case And: case Or:
1311 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001312 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001313 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001314 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001315 (isa<VectorType>(getType()) &&
1316 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001317 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001318 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001319 default:
1320 break;
1321 }
1322#endif
1323}
1324
1325BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001326 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001327 Instruction *InsertBefore) {
1328 assert(S1->getType() == S2->getType() &&
1329 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001330 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001331}
1332
1333BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001334 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001335 BasicBlock *InsertAtEnd) {
1336 BinaryOperator *Res = create(Op, S1, S2, Name);
1337 InsertAtEnd->getInstList().push_back(Res);
1338 return Res;
1339}
1340
1341BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1342 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001343 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1344 return new BinaryOperator(Instruction::Sub,
1345 zero, Op,
1346 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001347}
1348
1349BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1350 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001351 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1352 return new BinaryOperator(Instruction::Sub,
1353 zero, Op,
1354 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001355}
1356
1357BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1358 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001359 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001360 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001361 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001362 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001363 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001364 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001365 }
1366
1367 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001368 Op->getType(), Name, InsertBefore);
1369}
1370
1371BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1372 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001373 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001374 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001375 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001376 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001377 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001378 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001379 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001380 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001381 }
1382
1383 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001384 Op->getType(), Name, InsertAtEnd);
1385}
1386
1387
1388// isConstantAllOnes - Helper function for several functions below
1389static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001390 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1391 return CI->isAllOnesValue();
1392 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1393 return CV->isAllOnesValue();
1394 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001395}
1396
1397bool BinaryOperator::isNeg(const Value *V) {
1398 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1399 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001400 return Bop->getOperand(0) ==
1401 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001402 return false;
1403}
1404
1405bool BinaryOperator::isNot(const Value *V) {
1406 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1407 return (Bop->getOpcode() == Instruction::Xor &&
1408 (isConstantAllOnes(Bop->getOperand(1)) ||
1409 isConstantAllOnes(Bop->getOperand(0))));
1410 return false;
1411}
1412
Chris Lattner2c7d1772005-04-24 07:28:37 +00001413Value *BinaryOperator::getNegArgument(Value *BinOp) {
1414 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1415 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001416}
1417
Chris Lattner2c7d1772005-04-24 07:28:37 +00001418const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1419 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001420}
1421
Chris Lattner2c7d1772005-04-24 07:28:37 +00001422Value *BinaryOperator::getNotArgument(Value *BinOp) {
1423 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1424 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1425 Value *Op0 = BO->getOperand(0);
1426 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001427 if (isConstantAllOnes(Op0)) return Op1;
1428
1429 assert(isConstantAllOnes(Op1));
1430 return Op0;
1431}
1432
Chris Lattner2c7d1772005-04-24 07:28:37 +00001433const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1434 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001435}
1436
1437
1438// swapOperands - Exchange the two operands to this instruction. This
1439// instruction is safe to use on any binary instruction and does not
1440// modify the semantics of the instruction. If the instruction is
1441// order dependent (SetLT f.e.) the opcode is changed.
1442//
1443bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001444 if (!isCommutative())
1445 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001446 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001447 return false;
1448}
1449
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001450//===----------------------------------------------------------------------===//
1451// CastInst Class
1452//===----------------------------------------------------------------------===//
1453
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001454// Just determine if this cast only deals with integral->integral conversion.
1455bool CastInst::isIntegerCast() const {
1456 switch (getOpcode()) {
1457 default: return false;
1458 case Instruction::ZExt:
1459 case Instruction::SExt:
1460 case Instruction::Trunc:
1461 return true;
1462 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001463 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001464 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001465}
1466
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001467bool CastInst::isLosslessCast() const {
1468 // Only BitCast can be lossless, exit fast if we're not BitCast
1469 if (getOpcode() != Instruction::BitCast)
1470 return false;
1471
1472 // Identity cast is always lossless
1473 const Type* SrcTy = getOperand(0)->getType();
1474 const Type* DstTy = getType();
1475 if (SrcTy == DstTy)
1476 return true;
1477
Reid Spencer8d9336d2006-12-31 05:26:44 +00001478 // Pointer to pointer is always lossless.
1479 if (isa<PointerType>(SrcTy))
1480 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001481 return false; // Other types have no identity values
1482}
1483
1484/// This function determines if the CastInst does not require any bits to be
1485/// changed in order to effect the cast. Essentially, it identifies cases where
1486/// no code gen is necessary for the cast, hence the name no-op cast. For
1487/// example, the following are all no-op casts:
1488/// # bitcast uint %X, int
1489/// # bitcast uint* %x, sbyte*
Dan Gohmanfead7972007-05-11 21:43:24 +00001490/// # bitcast vector< 2 x int > %x, vector< 4 x short>
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001491/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1492/// @brief Determine if a cast is a no-op.
1493bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1494 switch (getOpcode()) {
1495 default:
1496 assert(!"Invalid CastOp");
1497 case Instruction::Trunc:
1498 case Instruction::ZExt:
1499 case Instruction::SExt:
1500 case Instruction::FPTrunc:
1501 case Instruction::FPExt:
1502 case Instruction::UIToFP:
1503 case Instruction::SIToFP:
1504 case Instruction::FPToUI:
1505 case Instruction::FPToSI:
1506 return false; // These always modify bits
1507 case Instruction::BitCast:
1508 return true; // BitCast never modifies bits.
1509 case Instruction::PtrToInt:
1510 return IntPtrTy->getPrimitiveSizeInBits() ==
1511 getType()->getPrimitiveSizeInBits();
1512 case Instruction::IntToPtr:
1513 return IntPtrTy->getPrimitiveSizeInBits() ==
1514 getOperand(0)->getType()->getPrimitiveSizeInBits();
1515 }
1516}
1517
1518/// This function determines if a pair of casts can be eliminated and what
1519/// opcode should be used in the elimination. This assumes that there are two
1520/// instructions like this:
1521/// * %F = firstOpcode SrcTy %x to MidTy
1522/// * %S = secondOpcode MidTy %F to DstTy
1523/// The function returns a resultOpcode so these two casts can be replaced with:
1524/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1525/// If no such cast is permited, the function returns 0.
1526unsigned CastInst::isEliminableCastPair(
1527 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1528 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1529{
1530 // Define the 144 possibilities for these two cast instructions. The values
1531 // in this matrix determine what to do in a given situation and select the
1532 // case in the switch below. The rows correspond to firstOp, the columns
1533 // correspond to secondOp. In looking at the table below, keep in mind
1534 // the following cast properties:
1535 //
1536 // Size Compare Source Destination
1537 // Operator Src ? Size Type Sign Type Sign
1538 // -------- ------------ ------------------- ---------------------
1539 // TRUNC > Integer Any Integral Any
1540 // ZEXT < Integral Unsigned Integer Any
1541 // SEXT < Integral Signed Integer Any
1542 // FPTOUI n/a FloatPt n/a Integral Unsigned
1543 // FPTOSI n/a FloatPt n/a Integral Signed
1544 // UITOFP n/a Integral Unsigned FloatPt n/a
1545 // SITOFP n/a Integral Signed FloatPt n/a
1546 // FPTRUNC > FloatPt n/a FloatPt n/a
1547 // FPEXT < FloatPt n/a FloatPt n/a
1548 // PTRTOINT n/a Pointer n/a Integral Unsigned
1549 // INTTOPTR n/a Integral Unsigned Pointer n/a
1550 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001551 //
1552 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1553 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1554 // into "fptoui double to ulong", but this loses information about the range
1555 // of the produced value (we no longer know the top-part is all zeros).
1556 // Further this conversion is often much more expensive for typical hardware,
1557 // and causes issues when building libgcc. We disallow fptosi+sext for the
1558 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001559 const unsigned numCastOps =
1560 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1561 static const uint8_t CastResults[numCastOps][numCastOps] = {
1562 // T F F U S F F P I B -+
1563 // R Z S P P I I T P 2 N T |
1564 // U E E 2 2 2 2 R E I T C +- secondOp
1565 // N X X U S F F N X N 2 V |
1566 // C T T I I P P C T T P T -+
1567 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1568 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1569 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001570 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1571 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001572 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1573 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1574 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1575 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1576 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1577 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1578 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1579 };
1580
1581 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1582 [secondOp-Instruction::CastOpsBegin];
1583 switch (ElimCase) {
1584 case 0:
1585 // categorically disallowed
1586 return 0;
1587 case 1:
1588 // allowed, use first cast's opcode
1589 return firstOp;
1590 case 2:
1591 // allowed, use second cast's opcode
1592 return secondOp;
1593 case 3:
1594 // no-op cast in second op implies firstOp as long as the DestTy
1595 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001596 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001597 return firstOp;
1598 return 0;
1599 case 4:
1600 // no-op cast in second op implies firstOp as long as the DestTy
1601 // is floating point
1602 if (DstTy->isFloatingPoint())
1603 return firstOp;
1604 return 0;
1605 case 5:
1606 // no-op cast in first op implies secondOp as long as the SrcTy
1607 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001608 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001609 return secondOp;
1610 return 0;
1611 case 6:
1612 // no-op cast in first op implies secondOp as long as the SrcTy
1613 // is a floating point
1614 if (SrcTy->isFloatingPoint())
1615 return secondOp;
1616 return 0;
1617 case 7: {
1618 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1619 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1620 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1621 if (MidSize >= PtrSize)
1622 return Instruction::BitCast;
1623 return 0;
1624 }
1625 case 8: {
1626 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1627 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1628 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1629 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1630 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1631 if (SrcSize == DstSize)
1632 return Instruction::BitCast;
1633 else if (SrcSize < DstSize)
1634 return firstOp;
1635 return secondOp;
1636 }
1637 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1638 return Instruction::ZExt;
1639 case 10:
1640 // fpext followed by ftrunc is allowed if the bit size returned to is
1641 // the same as the original, in which case its just a bitcast
1642 if (SrcTy == DstTy)
1643 return Instruction::BitCast;
1644 return 0; // If the types are not the same we can't eliminate it.
1645 case 11:
1646 // bitcast followed by ptrtoint is allowed as long as the bitcast
1647 // is a pointer to pointer cast.
1648 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1649 return secondOp;
1650 return 0;
1651 case 12:
1652 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1653 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1654 return firstOp;
1655 return 0;
1656 case 13: {
1657 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1658 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1659 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1660 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1661 if (SrcSize <= PtrSize && SrcSize == DstSize)
1662 return Instruction::BitCast;
1663 return 0;
1664 }
1665 case 99:
1666 // cast combination can't happen (error in input). This is for all cases
1667 // where the MidTy is not the same for the two cast instructions.
1668 assert(!"Invalid Cast Combination");
1669 return 0;
1670 default:
1671 assert(!"Error in CastResults table!!!");
1672 return 0;
1673 }
1674 return 0;
1675}
1676
1677CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1678 const std::string &Name, Instruction *InsertBefore) {
1679 // Construct and return the appropriate CastInst subclass
1680 switch (op) {
1681 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1682 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1683 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1684 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1685 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1686 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1687 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1688 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1689 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1690 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1691 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1692 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1693 default:
1694 assert(!"Invalid opcode provided");
1695 }
1696 return 0;
1697}
1698
1699CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1700 const std::string &Name, BasicBlock *InsertAtEnd) {
1701 // Construct and return the appropriate CastInst subclass
1702 switch (op) {
1703 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1704 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1705 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1706 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1707 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1708 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1709 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1710 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1711 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1712 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1713 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1714 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1715 default:
1716 assert(!"Invalid opcode provided");
1717 }
1718 return 0;
1719}
1720
Reid Spencer5c140882006-12-04 20:17:56 +00001721CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1722 const std::string &Name,
1723 Instruction *InsertBefore) {
1724 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1725 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1726 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1727}
1728
1729CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1730 const std::string &Name,
1731 BasicBlock *InsertAtEnd) {
1732 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1733 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1734 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1735}
1736
1737CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1738 const std::string &Name,
1739 Instruction *InsertBefore) {
1740 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1741 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1742 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1743}
1744
1745CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1746 const std::string &Name,
1747 BasicBlock *InsertAtEnd) {
1748 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1749 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1750 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1751}
1752
1753CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1754 const std::string &Name,
1755 Instruction *InsertBefore) {
1756 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1757 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1758 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1759}
1760
1761CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1762 const std::string &Name,
1763 BasicBlock *InsertAtEnd) {
1764 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1765 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1766 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1767}
1768
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001769CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1770 const std::string &Name,
1771 BasicBlock *InsertAtEnd) {
1772 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001773 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001774 "Invalid cast");
1775
Chris Lattner03c49532007-01-15 02:27:26 +00001776 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001777 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1778 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1779}
1780
1781/// @brief Create a BitCast or a PtrToInt cast instruction
1782CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1783 const std::string &Name,
1784 Instruction *InsertBefore) {
1785 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001786 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001787 "Invalid cast");
1788
Chris Lattner03c49532007-01-15 02:27:26 +00001789 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001790 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1791 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1792}
1793
Reid Spencer7e933472006-12-12 00:49:44 +00001794CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1795 bool isSigned, const std::string &Name,
1796 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001797 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001798 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1799 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1800 Instruction::CastOps opcode =
1801 (SrcBits == DstBits ? Instruction::BitCast :
1802 (SrcBits > DstBits ? Instruction::Trunc :
1803 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1804 return create(opcode, C, Ty, Name, InsertBefore);
1805}
1806
1807CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1808 bool isSigned, const std::string &Name,
1809 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001810 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001811 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1812 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1813 Instruction::CastOps opcode =
1814 (SrcBits == DstBits ? Instruction::BitCast :
1815 (SrcBits > DstBits ? Instruction::Trunc :
1816 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1817 return create(opcode, C, Ty, Name, InsertAtEnd);
1818}
1819
1820CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1821 const std::string &Name,
1822 Instruction *InsertBefore) {
1823 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1824 "Invalid cast");
1825 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1826 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1827 Instruction::CastOps opcode =
1828 (SrcBits == DstBits ? Instruction::BitCast :
1829 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1830 return create(opcode, C, Ty, Name, InsertBefore);
1831}
1832
1833CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1834 const std::string &Name,
1835 BasicBlock *InsertAtEnd) {
1836 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1837 "Invalid cast");
1838 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1839 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1840 Instruction::CastOps opcode =
1841 (SrcBits == DstBits ? Instruction::BitCast :
1842 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1843 return create(opcode, C, Ty, Name, InsertAtEnd);
1844}
1845
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001846// Provide a way to get a "cast" where the cast opcode is inferred from the
1847// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001848// logic in the castIsValid function below. This axiom should hold:
1849// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
1850// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001851// casting opcode for the arguments passed to it.
1852Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00001853CastInst::getCastOpcode(
1854 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001855 // Get the bit sizes, we'll need these
1856 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00001857 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1858 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001859
1860 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00001861 if (DestTy->isInteger()) { // Casting to integral
1862 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001863 if (DestBits < SrcBits)
1864 return Trunc; // int -> smaller int
1865 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00001866 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001867 return SExt; // signed -> SEXT
1868 else
1869 return ZExt; // unsigned -> ZEXT
1870 } else {
1871 return BitCast; // Same size, No-op cast
1872 }
1873 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00001874 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001875 return FPToSI; // FP -> sint
1876 else
1877 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00001878 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001879 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00001880 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001881 return BitCast; // Same size, no-op cast
1882 } else {
1883 assert(isa<PointerType>(SrcTy) &&
1884 "Casting from a value that is not first-class type");
1885 return PtrToInt; // ptr -> int
1886 }
1887 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00001888 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00001889 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001890 return SIToFP; // sint -> FP
1891 else
1892 return UIToFP; // uint -> FP
1893 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1894 if (DestBits < SrcBits) {
1895 return FPTrunc; // FP -> smaller FP
1896 } else if (DestBits > SrcBits) {
1897 return FPExt; // FP -> larger FP
1898 } else {
1899 return BitCast; // same size, no-op cast
1900 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001901 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001902 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00001903 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001904 return BitCast; // same size, no-op cast
1905 } else {
1906 assert(0 && "Casting pointer or non-first class to float");
1907 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001908 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
1909 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001910 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00001911 "Casting vector to vector of different widths");
1912 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001913 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00001914 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001915 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00001916 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001917 }
1918 } else if (isa<PointerType>(DestTy)) {
1919 if (isa<PointerType>(SrcTy)) {
1920 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00001921 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001922 return IntToPtr; // int -> ptr
1923 } else {
1924 assert(!"Casting pointer to other than pointer or int");
1925 }
1926 } else {
1927 assert(!"Casting to type that is not first-class");
1928 }
1929
1930 // If we fall through to here we probably hit an assertion cast above
1931 // and assertions are not turned on. Anything we return is an error, so
1932 // BitCast is as good a choice as any.
1933 return BitCast;
1934}
1935
1936//===----------------------------------------------------------------------===//
1937// CastInst SubClass Constructors
1938//===----------------------------------------------------------------------===//
1939
1940/// Check that the construction parameters for a CastInst are correct. This
1941/// could be broken out into the separate constructors but it is useful to have
1942/// it in one place and to eliminate the redundant code for getting the sizes
1943/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001944bool
1945CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001946
1947 // Check for type sanity on the arguments
1948 const Type *SrcTy = S->getType();
1949 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
1950 return false;
1951
1952 // Get the size of the types in bits, we'll need this later
1953 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1954 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
1955
1956 // Switch on the opcode provided
1957 switch (op) {
1958 default: return false; // This is an input error
1959 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00001960 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001961 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001962 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001963 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001964 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001965 case Instruction::FPTrunc:
1966 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1967 SrcBitSize > DstBitSize;
1968 case Instruction::FPExt:
1969 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1970 SrcBitSize < DstBitSize;
1971 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001972 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00001973 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
1974 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
1975 return SVTy->getElementType()->isInteger() &&
1976 DVTy->getElementType()->isFloatingPoint() &&
1977 SVTy->getNumElements() == DVTy->getNumElements();
1978 }
1979 }
Chris Lattner03c49532007-01-15 02:27:26 +00001980 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001981 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001982 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00001983 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
1984 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
1985 return SVTy->getElementType()->isFloatingPoint() &&
1986 DVTy->getElementType()->isInteger() &&
1987 SVTy->getNumElements() == DVTy->getNumElements();
1988 }
1989 }
Chris Lattner03c49532007-01-15 02:27:26 +00001990 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001991 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00001992 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001993 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00001994 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001995 case Instruction::BitCast:
1996 // BitCast implies a no-op cast of type only. No bits change.
1997 // However, you can't cast pointers to anything but pointers.
1998 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
1999 return false;
2000
2001 // Now we know we're not dealing with a pointer/non-poiner mismatch. In all
2002 // these cases, the cast is okay if the source and destination bit widths
2003 // are identical.
2004 return SrcBitSize == DstBitSize;
2005 }
2006}
2007
2008TruncInst::TruncInst(
2009 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2010) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002011 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002012}
2013
2014TruncInst::TruncInst(
2015 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2016) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002017 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002018}
2019
2020ZExtInst::ZExtInst(
2021 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2022) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002023 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002024}
2025
2026ZExtInst::ZExtInst(
2027 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2028) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002029 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002030}
2031SExtInst::SExtInst(
2032 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2033) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002034 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002035}
2036
Jeff Cohencc08c832006-12-02 02:22:01 +00002037SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002038 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2039) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002040 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002041}
2042
2043FPTruncInst::FPTruncInst(
2044 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2045) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002046 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002047}
2048
2049FPTruncInst::FPTruncInst(
2050 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2051) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002052 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002053}
2054
2055FPExtInst::FPExtInst(
2056 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2057) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002058 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002059}
2060
2061FPExtInst::FPExtInst(
2062 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2063) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002064 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002065}
2066
2067UIToFPInst::UIToFPInst(
2068 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2069) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002070 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002071}
2072
2073UIToFPInst::UIToFPInst(
2074 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2075) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002076 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002077}
2078
2079SIToFPInst::SIToFPInst(
2080 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2081) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002082 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002083}
2084
2085SIToFPInst::SIToFPInst(
2086 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2087) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002088 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002089}
2090
2091FPToUIInst::FPToUIInst(
2092 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2093) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002094 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002095}
2096
2097FPToUIInst::FPToUIInst(
2098 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2099) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002100 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002101}
2102
2103FPToSIInst::FPToSIInst(
2104 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2105) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002106 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002107}
2108
2109FPToSIInst::FPToSIInst(
2110 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2111) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002112 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002113}
2114
2115PtrToIntInst::PtrToIntInst(
2116 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2117) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002118 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002119}
2120
2121PtrToIntInst::PtrToIntInst(
2122 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2123) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002124 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002125}
2126
2127IntToPtrInst::IntToPtrInst(
2128 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2129) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002130 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002131}
2132
2133IntToPtrInst::IntToPtrInst(
2134 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2135) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002136 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002137}
2138
2139BitCastInst::BitCastInst(
2140 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2141) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002142 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002143}
2144
2145BitCastInst::BitCastInst(
2146 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2147) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002148 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002149}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002150
2151//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002152// CmpInst Classes
2153//===----------------------------------------------------------------------===//
2154
2155CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2156 const std::string &Name, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00002157 : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002158 Ops[0].init(LHS, this);
2159 Ops[1].init(RHS, this);
2160 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002161 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002162 if (op == Instruction::ICmp) {
2163 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2164 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2165 "Invalid ICmp predicate value");
2166 const Type* Op0Ty = getOperand(0)->getType();
2167 const Type* Op1Ty = getOperand(1)->getType();
2168 assert(Op0Ty == Op1Ty &&
2169 "Both operands to ICmp instruction are not of the same type!");
2170 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002171 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002172 "Invalid operand types for ICmp instruction");
2173 return;
2174 }
2175 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2176 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2177 "Invalid FCmp predicate value");
2178 const Type* Op0Ty = getOperand(0)->getType();
2179 const Type* Op1Ty = getOperand(1)->getType();
2180 assert(Op0Ty == Op1Ty &&
2181 "Both operands to FCmp instruction are not of the same type!");
2182 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002183 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002184 "Invalid operand types for FCmp instruction");
2185}
2186
2187CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2188 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00002189 : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002190 Ops[0].init(LHS, this);
2191 Ops[1].init(RHS, this);
2192 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002193 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002194 if (op == Instruction::ICmp) {
2195 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2196 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2197 "Invalid ICmp predicate value");
2198
2199 const Type* Op0Ty = getOperand(0)->getType();
2200 const Type* Op1Ty = getOperand(1)->getType();
2201 assert(Op0Ty == Op1Ty &&
2202 "Both operands to ICmp instruction are not of the same type!");
2203 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002204 assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002205 "Invalid operand types for ICmp instruction");
2206 return;
2207 }
2208 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2209 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2210 "Invalid FCmp predicate value");
2211 const Type* Op0Ty = getOperand(0)->getType();
2212 const Type* Op1Ty = getOperand(1)->getType();
2213 assert(Op0Ty == Op1Ty &&
2214 "Both operands to FCmp instruction are not of the same type!");
2215 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002216 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002217 "Invalid operand types for FCmp instruction");
2218}
2219
2220CmpInst *
2221CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2222 const std::string &Name, Instruction *InsertBefore) {
2223 if (Op == Instruction::ICmp) {
2224 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2225 InsertBefore);
2226 }
2227 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2228 InsertBefore);
2229}
2230
2231CmpInst *
2232CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2233 const std::string &Name, BasicBlock *InsertAtEnd) {
2234 if (Op == Instruction::ICmp) {
2235 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2236 InsertAtEnd);
2237 }
2238 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2239 InsertAtEnd);
2240}
2241
2242void CmpInst::swapOperands() {
2243 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2244 IC->swapOperands();
2245 else
2246 cast<FCmpInst>(this)->swapOperands();
2247}
2248
2249bool CmpInst::isCommutative() {
2250 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2251 return IC->isCommutative();
2252 return cast<FCmpInst>(this)->isCommutative();
2253}
2254
2255bool CmpInst::isEquality() {
2256 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2257 return IC->isEquality();
2258 return cast<FCmpInst>(this)->isEquality();
2259}
2260
2261
2262ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2263 switch (pred) {
2264 default:
2265 assert(!"Unknown icmp predicate!");
2266 case ICMP_EQ: return ICMP_NE;
2267 case ICMP_NE: return ICMP_EQ;
2268 case ICMP_UGT: return ICMP_ULE;
2269 case ICMP_ULT: return ICMP_UGE;
2270 case ICMP_UGE: return ICMP_ULT;
2271 case ICMP_ULE: return ICMP_UGT;
2272 case ICMP_SGT: return ICMP_SLE;
2273 case ICMP_SLT: return ICMP_SGE;
2274 case ICMP_SGE: return ICMP_SLT;
2275 case ICMP_SLE: return ICMP_SGT;
2276 }
2277}
2278
2279ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2280 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002281 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002282 case ICMP_EQ: case ICMP_NE:
2283 return pred;
2284 case ICMP_SGT: return ICMP_SLT;
2285 case ICMP_SLT: return ICMP_SGT;
2286 case ICMP_SGE: return ICMP_SLE;
2287 case ICMP_SLE: return ICMP_SGE;
2288 case ICMP_UGT: return ICMP_ULT;
2289 case ICMP_ULT: return ICMP_UGT;
2290 case ICMP_UGE: return ICMP_ULE;
2291 case ICMP_ULE: return ICMP_UGE;
2292 }
2293}
2294
Reid Spencer266e42b2006-12-23 06:05:41 +00002295ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2296 switch (pred) {
2297 default: assert(! "Unknown icmp predicate!");
2298 case ICMP_EQ: case ICMP_NE:
2299 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2300 return pred;
2301 case ICMP_UGT: return ICMP_SGT;
2302 case ICMP_ULT: return ICMP_SLT;
2303 case ICMP_UGE: return ICMP_SGE;
2304 case ICMP_ULE: return ICMP_SLE;
2305 }
2306}
2307
2308bool ICmpInst::isSignedPredicate(Predicate pred) {
2309 switch (pred) {
2310 default: assert(! "Unknown icmp predicate!");
2311 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2312 return true;
2313 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2314 case ICMP_UGE: case ICMP_ULE:
2315 return false;
2316 }
2317}
2318
Reid Spencer0286bc12007-02-28 22:00:54 +00002319/// Initialize a set of values that all satisfy the condition with C.
2320///
2321ConstantRange
2322ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2323 APInt Lower(C);
2324 APInt Upper(C);
2325 uint32_t BitWidth = C.getBitWidth();
2326 switch (pred) {
2327 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2328 case ICmpInst::ICMP_EQ: Upper++; break;
2329 case ICmpInst::ICMP_NE: Lower++; break;
2330 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2331 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2332 case ICmpInst::ICMP_UGT:
2333 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2334 break;
2335 case ICmpInst::ICMP_SGT:
2336 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2337 break;
2338 case ICmpInst::ICMP_ULE:
2339 Lower = APInt::getMinValue(BitWidth); Upper++;
2340 break;
2341 case ICmpInst::ICMP_SLE:
2342 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2343 break;
2344 case ICmpInst::ICMP_UGE:
2345 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2346 break;
2347 case ICmpInst::ICMP_SGE:
2348 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2349 break;
2350 }
2351 return ConstantRange(Lower, Upper);
2352}
2353
Reid Spencerd9436b62006-11-20 01:22:35 +00002354FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2355 switch (pred) {
2356 default:
2357 assert(!"Unknown icmp predicate!");
2358 case FCMP_OEQ: return FCMP_UNE;
2359 case FCMP_ONE: return FCMP_UEQ;
2360 case FCMP_OGT: return FCMP_ULE;
2361 case FCMP_OLT: return FCMP_UGE;
2362 case FCMP_OGE: return FCMP_ULT;
2363 case FCMP_OLE: return FCMP_UGT;
2364 case FCMP_UEQ: return FCMP_ONE;
2365 case FCMP_UNE: return FCMP_OEQ;
2366 case FCMP_UGT: return FCMP_OLE;
2367 case FCMP_ULT: return FCMP_OGE;
2368 case FCMP_UGE: return FCMP_OLT;
2369 case FCMP_ULE: return FCMP_OGT;
2370 case FCMP_ORD: return FCMP_UNO;
2371 case FCMP_UNO: return FCMP_ORD;
2372 case FCMP_TRUE: return FCMP_FALSE;
2373 case FCMP_FALSE: return FCMP_TRUE;
2374 }
2375}
2376
2377FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2378 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002379 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002380 case FCMP_FALSE: case FCMP_TRUE:
2381 case FCMP_OEQ: case FCMP_ONE:
2382 case FCMP_UEQ: case FCMP_UNE:
2383 case FCMP_ORD: case FCMP_UNO:
2384 return pred;
2385 case FCMP_OGT: return FCMP_OLT;
2386 case FCMP_OLT: return FCMP_OGT;
2387 case FCMP_OGE: return FCMP_OLE;
2388 case FCMP_OLE: return FCMP_OGE;
2389 case FCMP_UGT: return FCMP_ULT;
2390 case FCMP_ULT: return FCMP_UGT;
2391 case FCMP_UGE: return FCMP_ULE;
2392 case FCMP_ULE: return FCMP_UGE;
2393 }
2394}
2395
Reid Spencer266e42b2006-12-23 06:05:41 +00002396bool CmpInst::isUnsigned(unsigned short predicate) {
2397 switch (predicate) {
2398 default: return false;
2399 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2400 case ICmpInst::ICMP_UGE: return true;
2401 }
2402}
2403
2404bool CmpInst::isSigned(unsigned short predicate){
2405 switch (predicate) {
2406 default: return false;
2407 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2408 case ICmpInst::ICMP_SGE: return true;
2409 }
2410}
2411
2412bool CmpInst::isOrdered(unsigned short predicate) {
2413 switch (predicate) {
2414 default: return false;
2415 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2416 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2417 case FCmpInst::FCMP_ORD: return true;
2418 }
2419}
2420
2421bool CmpInst::isUnordered(unsigned short predicate) {
2422 switch (predicate) {
2423 default: return false;
2424 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2425 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2426 case FCmpInst::FCMP_UNO: return true;
2427 }
2428}
2429
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002430//===----------------------------------------------------------------------===//
2431// SwitchInst Implementation
2432//===----------------------------------------------------------------------===//
2433
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002434void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002435 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002436 ReservedSpace = 2+NumCases*2;
2437 NumOperands = 2;
2438 OperandList = new Use[ReservedSpace];
2439
2440 OperandList[0].init(Value, this);
2441 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002442}
2443
Chris Lattner2195fc42007-02-24 00:55:48 +00002444/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2445/// switch on and a default destination. The number of additional cases can
2446/// be specified here to make memory allocation more efficient. This
2447/// constructor can also autoinsert before another instruction.
2448SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2449 Instruction *InsertBefore)
2450 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2451 init(Value, Default, NumCases);
2452}
2453
2454/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2455/// switch on and a default destination. The number of additional cases can
2456/// be specified here to make memory allocation more efficient. This
2457/// constructor also autoinserts at the end of the specified BasicBlock.
2458SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2459 BasicBlock *InsertAtEnd)
2460 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2461 init(Value, Default, NumCases);
2462}
2463
Misha Brukmanb1c93172005-04-21 23:48:37 +00002464SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002465 : TerminatorInst(Type::VoidTy, Instruction::Switch,
2466 new Use[SI.getNumOperands()], SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002467 Use *OL = OperandList, *InOL = SI.OperandList;
2468 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2469 OL[i].init(InOL[i], this);
2470 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002471 }
2472}
2473
Gordon Henriksen14a55692007-12-10 02:14:30 +00002474SwitchInst::~SwitchInst() {
2475 delete [] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002476}
2477
2478
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002479/// addCase - Add an entry to the switch instruction...
2480///
Chris Lattner47ac1872005-02-24 05:32:09 +00002481void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002482 unsigned OpNo = NumOperands;
2483 if (OpNo+2 > ReservedSpace)
2484 resizeOperands(0); // Get more space!
2485 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002486 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002487 NumOperands = OpNo+2;
2488 OperandList[OpNo].init(OnVal, this);
2489 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002490}
2491
2492/// removeCase - This method removes the specified successor from the switch
2493/// instruction. Note that this cannot be used to remove the default
2494/// destination (successor #0).
2495///
2496void SwitchInst::removeCase(unsigned idx) {
2497 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002498 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2499
2500 unsigned NumOps = getNumOperands();
2501 Use *OL = OperandList;
2502
2503 // Move everything after this operand down.
2504 //
2505 // FIXME: we could just swap with the end of the list, then erase. However,
2506 // client might not expect this to happen. The code as it is thrashes the
2507 // use/def lists, which is kinda lame.
2508 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2509 OL[i-2] = OL[i];
2510 OL[i-2+1] = OL[i+1];
2511 }
2512
2513 // Nuke the last value.
2514 OL[NumOps-2].set(0);
2515 OL[NumOps-2+1].set(0);
2516 NumOperands = NumOps-2;
2517}
2518
2519/// resizeOperands - resize operands - This adjusts the length of the operands
2520/// list according to the following behavior:
2521/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2522/// of operation. This grows the number of ops by 1.5 times.
2523/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2524/// 3. If NumOps == NumOperands, trim the reserved space.
2525///
2526void SwitchInst::resizeOperands(unsigned NumOps) {
2527 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002528 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002529 } else if (NumOps*2 > NumOperands) {
2530 // No resize needed.
2531 if (ReservedSpace >= NumOps) return;
2532 } else if (NumOps == NumOperands) {
2533 if (ReservedSpace == NumOps) return;
2534 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002535 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002536 }
2537
2538 ReservedSpace = NumOps;
2539 Use *NewOps = new Use[NumOps];
2540 Use *OldOps = OperandList;
2541 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2542 NewOps[i].init(OldOps[i], this);
2543 OldOps[i].set(0);
2544 }
2545 delete [] OldOps;
2546 OperandList = NewOps;
2547}
2548
2549
2550BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2551 return getSuccessor(idx);
2552}
2553unsigned SwitchInst::getNumSuccessorsV() const {
2554 return getNumSuccessors();
2555}
2556void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2557 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002558}
Chris Lattnerf22be932004-10-15 23:52:53 +00002559
2560
2561// Define these methods here so vtables don't get emitted into every translation
2562// unit that uses these classes.
2563
2564GetElementPtrInst *GetElementPtrInst::clone() const {
2565 return new GetElementPtrInst(*this);
2566}
2567
2568BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002569 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002570}
2571
Chris Lattner0b490b02007-08-24 20:48:18 +00002572FCmpInst* FCmpInst::clone() const {
2573 return new FCmpInst(getPredicate(), Ops[0], Ops[1]);
2574}
2575ICmpInst* ICmpInst::clone() const {
2576 return new ICmpInst(getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002577}
2578
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002579MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2580AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2581FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2582LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2583StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2584CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2585CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2586CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2587CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2588CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2589CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2590CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2591CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2592CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2593CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2594CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2595CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2596CallInst *CallInst::clone() const { return new CallInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002597SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2598VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2599
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002600ExtractElementInst *ExtractElementInst::clone() const {
2601 return new ExtractElementInst(*this);
2602}
2603InsertElementInst *InsertElementInst::clone() const {
2604 return new InsertElementInst(*this);
2605}
2606ShuffleVectorInst *ShuffleVectorInst::clone() const {
2607 return new ShuffleVectorInst(*this);
2608}
Chris Lattnerf22be932004-10-15 23:52:53 +00002609PHINode *PHINode::clone() const { return new PHINode(*this); }
2610ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2611BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2612SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2613InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2614UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002615UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}